-
Notifications
You must be signed in to change notification settings - Fork 0
/
gurl.js
71 lines (61 loc) · 1.88 KB
/
gurl.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
function gurlClick(info, tab) {
if (info.menuItemId == "gurlCopy") {
var url = gurlGetOrigin(info.linkUrl);
console.log(url);
gurlClip(url);
return;
}
if (info.menuItemId == "gurlOpen") {
var url = gurlGetOrigin(info.linkUrl);
console.log(url);
chrome.tabs.create({"url":url}, null);
return;
}
}
function gurlParser(url) {
var a = document.createElement('a');
a.href = url;
return a;
}
function gurlIsGoogle(url) {
var a = gurlParser(url);
console.log(a.hostname);
var ret = false;
if (a.hostname.search("google") >= 0)
ret = true;
return ret;
}
function gurlGetOrigin(url) {
var a = gurlParser(url);
var seg = a.search.replace(/^\?/,'').split('&');
for (var i=0;i<seg.length;i++) {
if (!seg[i])
continue;
s = seg[i].split('=');
if (s[0] == "url")
return decodeURIComponent(s[1]);
}
return null;
}
function gurlClip(url) {
if (url == null)
return;
var t = document.createElement("textarea");
var bodys = document.getElementsByTagName("body");
if (bodys == null || 0 >= bodys.length)
return;
var body = bodys[0];
body.appendChild(t);
t.value = url;
t.select();
document.execCommand("copy");
body.removeChild(t);
}
var documentUrlPatterns = ["*://*.google.com/*", "*://*.google.com.hk/*", "*://*.google.com.tw/*", "*://*.google.co.jp/*", "*://*.google.co.uk/*"];
var contexts = ["link"];
var menus = [
{ "id":"gurlCopy", "title": "copy original url", "contexts": contexts, "documentUrlPatterns": documentUrlPatterns, "onclick": gurlClick },
{ "id":"gurlOpen", "title": "open original url", "contexts": contexts, "documentUrlPatterns": documentUrlPatterns, "onclick": gurlClick }
];
// chrome.contextMenus.create(menus[0], null);
chrome.contextMenus.create(menus[1], null);