-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
122 lines (103 loc) · 3.66 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var userAgent = navigator.userAgent; // getting the useragent for determining the browser
var platform = "";
if (userAgent.includes(" Edg")){
platform = "Edge"
} else if (userAgent.includes(" Chrome")){
platform = "Chrome"
}
document.querySelector("#platform").innerHTML = platform; // adding the platform to the ext.
// Querying the tabs
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
let thisTab = tabs[0]; // Selecting the current tab
document.getElementById("title").innerHTML = thisTab.title; // currTab -> title
document.getElementById("url").innerHTML = thisTab.url; // currTab -> url
});
function getMetas() {
// Injecting a script onto a page
chrome.tabs.executeScript(null, {
file: "getMetaData.js" // gets the metadata
}, function() {
if (chrome.runtime.lastError){
console.log("Error Occured");
}
});
}
// Checking for a message from the injected scripts
chrome.runtime.onMessage.addListener(function(request, sender){
if(request.method == "getMetaData"){
document.getElementById("site_title").innerHTML = request.siteName
}
});
window.onload = getMetas; // Injecting the script after the page loads completely
// querying copy buttons to add event
let copyBTNS = document.querySelectorAll(".cbtn");
for (let i = 0; i < copyBTNS.length; i++){
// adding copy event to the copy buttons
copyBTNS[i].addEventListener("click", (event) => {
copyTitle2(event);
});
}
/**
* selects and copies the desired text
* @param {event} e
*/
function copyTitle2(e){
let whichBtn = e.target;
// Selects the BUTTON element if the user
// clicks on the IMG inside the BUTTON node
if (whichBtn.tagName == "IMG"){
whichBtn = whichBtn.parentElement; // The button is the parent to the img element
}
/**
* Here the content of the target element id determined according to the
* ID of the button
*/
whichBtn = whichBtn.getAttribute("id");
if(whichBtn == "titleButton"){
let thisText = document.getElementById("title").innerHTML;
copyText(thisText);
} else if (whichBtn == "siteButton"){
try {
let thisText = document.getElementById("site_title").innerHTML;
copyText(thisText);
} catch (TypeError) {
copyText("");
}
} else if (whichBtn == "urlButton"){
let thisText = document.getElementById("url").innerHTML;
copyText(thisText);
}else {
console.log("Unknown Button")
}
}
/**
* copies the parameter to the clip board
*
* @param {string} text
*/
function copyText(text){
// Displays the appropriate toast if the field is empty
if (text == ""){
let toast = document.getElementById("toast");
toast.innerHTML = "Empty String!"
toast.className = "show";
setTimeout(function() {
toast.className = toast.className.replace("show", "");
}, 3000);
} else {
var dummy = document.createElement("input"); // Creates an input element
document.body.appendChild(dummy); // adds the element to the body
dummy.setAttribute("id", "dummy_id"); // sets the if og the element
document.getElementById("dummy_id").value = text; // gets the input element and sets its value to parameter
dummy.select(); // selects the content of the element
document.execCommand("copy"); // executes the copy command Ctrl+C
document.body.removeChild(dummy); // Removes the input element
let toast = document.getElementById("toast"); // selects the toast
toast.innerHTML = "Copied To Clipboard!"; // sets the text of the toast
toast.className = "show"; // shows the toats
// Hides the toast after 3 seconds = 3000 ms
setTimeout(function() {
toast.className = toast.className.replace("show", "");
}, 3000);
}
}