-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpWURL.js
49 lines (42 loc) · 1.29 KB
/
cpWURL.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
function copyWithURL( info , is_strip_query_string ) {
var page_url = info.pageUrl
// Strip off query string
if ( is_strip_query_string ) {
var qs_idx = page_url.indexOf('?');
if ( qs_idx != -1 ){
var page_url = page_url.substring(0, qs_idx);
}
}
// Append URL to text selection
var copyText = info.selectionText + "\n" + page_url;
console.log( copyText );
// Copy text to clipboard
copyToClipboard( copyText );
}
function copyToClipboard( text ){
var copyPre = document.createElement('pre');
copyPre.contentEditable = true;
document.body.appendChild(copyPre);
copyPre.innerHTML = text;
copyPre.unselectable = "off";
copyPre.focus();
document.execCommand('SelectAll');
document.execCommand("Copy", false, null);
document.body.removeChild(copyPre);
}
// Copy with URL Only
chrome.contextMenus.create({
title: "Copy with URL",
contexts:["selection"],
onclick: function(info, tab) {
copyWithURL( info, false )
}
});
// Copy with URL minus Query String
chrome.contextMenus.create({
title: "Copy with URL minus query string",
contexts:["selection"],
onclick: function(info, tab) {
copyWithURL( info, true )
}
});