-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyurl.js
113 lines (97 loc) · 3.43 KB
/
copyurl.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
// FW 12.6.2012
function addSelection(selection,str) {
var clipboard = document.getElementById("clipboard");
clipboard.innerHTML = str;
var range = document.createRange();
range.setStartBefore(clipboard);
range.setEndAfter(clipboard);
range.selectNode(clipboard);
selection.addRange(range);
}
function getNice(format,url,title,screenshotUrl) {
var nice = url;
if ( format==childWiki ) {
nice = "[["+url+"]["+title+"]]";
} else if ( format==childHtml ) {
nice = "<a href='"+url+"'>"+title+"</a>";
} else if ( format==childHtmlScreenshot ) {
nice = "<a href='"+url+"'>"+title+"</a><br/><img src='"+screenshotUrl+"' width='100%'/>";
}
return nice;
}
function copyUrl(format,url,title,screenshotUrl) {
var nice = getNice(format,url,title,screenshotUrl);
var selection = window.getSelection();
selection.removeAllRanges();
addSelection(selection,nice);
document.execCommand("copy");
}
function copyUrls(format,urls) {
var nl = '<br/>';
var allNice = "";
for (var i=0; i<urls.length; i++) {
var nice = getNice(format,urls[i].url,urls[i].title);
allNice += nice + nl;
}
var selection = window.getSelection();
selection.removeAllRanges();
addSelection(selection,allNice);
document.execCommand("copy");
}
function copyUrlFindTitle(format,byurl,tabid) {
chrome.tabs.executeScript(tabid,{file:"content.js"}, function() {
chrome.tabs.sendRequest(tabid, {action:"getUrlTitle", url:byurl}, function(title) {
copyUrl(format,byurl,title);
});
});
}
function copyUrlFindSelected(format,tabid) {
chrome.tabs.executeScript(tabid,{file:"content.js"}, function() {
chrome.tabs.sendRequest(tabid, {action:"getSelectedUrls"}, function(urls) {
copyUrls(format,urls);
});
});
}
function copyUrlWithScreenshot(format,byurl,title) {
chrome.tabs.captureVisibleTab(null,{format:"png"},function(url) {
copyUrl(format,byurl,title,url);
});
}
function mailClipboard(title,tabid) {
var mail = document.getElementById("mail");
mail.action="mailto:?subject="+encodeURIComponent(title);
var body = document.getElementById("clipboard").innerHTML;
mail.elements[0].value=body;
document.forms['mail'].submit();
}
// A generic onclick callback function.
function genericOnClick(info, tab) {
//console.log("item " + info.menuItemId + " was clicked");
if ( info.linkUrl ) {
copyUrlFindTitle(info.menuItemId,info.linkUrl,tab.id);
return;
}
if ( info.selectionText ) {
copyUrlFindSelected(info.menuItemId,tab.id);
return;
}
copyUrl(info.menuItemId,tab.url,tab.title);
}
function screenshotOnClick(info, tab) {
copyUrlWithScreenshot(childHtmlScreenshot,tab.url,tab.title);
}
function mailOnClick(info, tab) {
screenshotOnClick(info,tab);
mailClipboard(tab.title,tab.id);
}
// Create a parent item and two children.
var contexts = ["page","link","selection"];
var parent = chrome.contextMenus.create({"title": "Copy URL nicely", "contexts":contexts});
var childHtml = chrome.contextMenus.create(
{"title": "as Html", "parentId": parent, "onclick": genericOnClick, "contexts":contexts});
var childWiki = chrome.contextMenus.create(
{"title": "as Wiki", "parentId": parent, "onclick": genericOnClick, "contexts":contexts});
var childHtmlScreenshot = chrome.contextMenus.create(
{"title": "as Html with Screenshot", "parentId": parent, "onclick": screenshotOnClick});
var childMail = chrome.contextMenus.create(
{"title": "as Mail", "parentId": parent, "onclick": mailOnClick});