-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmenu.js
41 lines (36 loc) · 1.47 KB
/
menu.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
function extractLinkData (linkNode, page) {
return {
"linktext" : linkNode.innerText,
"linkurl" : linkNode.href,
"linktitle" : linkNode.title,
"pageurl" : page.location ? page.location.href : undefined,
"pagetitle" : page.title
}
}
var copiedClass = "copy-link-text-copied";
var copiedClassRegex = new RegExp("\\b"+copiedClass+"\\b", "g");
// listener is on the document so that we also handle links added after page load
document.addEventListener( "contextmenu", function(event) {
var node = event.target;
// handle nested tags within the link
// let's try and not hang the browser for a really complex dom tree
// -- hopefully we don't have stuff THIS nested
var limit = 5;
while ( node && ( limit-- > 0 ) ) {
if ( node.nodeName === "A" && node.href != undefined ) {
chrome.extension.sendRequest(extractLinkData(node, document), function(msg){
if(msg && msg.action == "highlightCopied"){
var oldCopied = document.getElementsByClassName(copiedClass)
for ( var i = 0; i < oldCopied.length; i++ ) {
oldCopied[i].className
= oldCopied[i].className.replace( copiedClassRegex, "" );
}
node.className += " " + copiedClass;
}
});
return true;
} else {
node = node.parentNode;
}
}
}, false );