-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
67 lines (53 loc) · 2.3 KB
/
background.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
chrome.contextMenus.removeAll(function() {
chrome.contextMenus.create({
id: "checkMenu",
title: "check this",
contexts: ["all"]
});
});
function copyTextToClipboard(text) {
//Create a textbox field where we can insert text to.
var copyFrom = document.createElement("textarea");
//Set the text content to be the text you wished to copy.
copyFrom.textContent = text;
//Append the textbox field into the body as a child.
//"execCommand()" only works when there exists selected text, and the text is inside
//document.body (meaning the text is part of a valid rendered HTML element).
document.body.appendChild(copyFrom);
//Select all the text!
copyFrom.select();
//Execute command
document.execCommand('copy');
//(Optional) De-select the text using blur().
copyFrom.blur();
//Remove the textbox field from the document.body, so no other JavaScript nor
//other elements can get access to this.
document.body.removeChild(copyFrom);
}
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId == "checkMenu") {
chrome.tabs.executeScript(null, {file: "select.js"});
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if(message.type == "notification" && message.options.title == "path"){
path = message.options.message;
var crypted_path = window.btoa(path);
copyTextToClipboard(info.pageUrl+"?weber="+crypted_path);
}
});
}
});
chrome.tabs.onUpdated.addListener( function( tabId, changeInfo, tab) {
if (changeInfo.status == 'complete') {
var regex = new RegExp("weber=*");
//chrome.extension.getBackgroundPage();
if(tab.url.match(regex)){
var current_encode_div = decodeURIComponent(tab.url.split('weber=')[1]);
current_encode_div = current_encode_div.replace("?","")
current_div = window.atob(current_encode_div);
chrome.tabs.executeScript(null,{code:`
document.querySelector('${current_div}').style.backgroundColor = 'chartreuse';
document.querySelector('${current_div}').scrollIntoView({ alignToTop: 'false',behavior: 'smooth', block: 'start', inline: 'start' });
`});
}
}
});