-
Notifications
You must be signed in to change notification settings - Fork 0
/
bg.js
113 lines (96 loc) · 2.23 KB
/
bg.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
var options;
var timeout;
var elem = document.createElement('input');
document.body.appendChild(elem);
function copy(text) {
if (options.clean_url) {
elem.value = cleanURL(text);
} else {
elem.value = text;
}
elem.select();
document.execCommand('Copy', false, null);
}
function setIcon(icon) {
chrome.browserAction.setIcon({
path: 'copy_' + icon + '_128.png'
});
}
function setBadgeText(text) {
chrome.browserAction.setBadgeText({
text: text
});
}
function cleanURL(url) {
var a = document.createElement('a');
a.href = url;
a.search = removeTrackingTags(a.search.replace(/^\?/,''));
a.hash = removeTrackingTags(a.hash.replace(/^#/,''));
return a.href;
}
function removeTrackingTags(str) {
return str
.split('&')
.filter(function(item) {
return !/^(utm_|from=|_openstat)/.test(item);
})
.join('&');
}
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({
'id': 'copy-page-url',
'title': 'Copy Page URL',
'contexts':[
'page',
'selection',
'link',
'editable',
'image',
'video',
'audio'
]
});
chrome.contextMenus.create({
'id': 'copy-frame-url',
'title': 'Copy Frame URL',
'contexts':[
'frame'
]
});
chrome.browserAction.setBadgeBackgroundColor({
color: '#32cd32'
});
});
chrome.contextMenus.onClicked.addListener(function(info) {
if (info.menuItemId === 'copy-page-url') {
copy(info.pageUrl);
} else if (info.menuItemId === 'copy-frame-url') {
copy(info.frameUrl);
}
});
chrome.browserAction.onClicked.addListener(function(tab) {
copy(tab.url);
setBadgeText('OK!');
clearTimeout(timeout);
timeout = setTimeout(setBadgeText.bind(null, ''), 1000);
});
chrome.tabs.onActivated.addListener(function() {
clearTimeout(timeout);
setBadgeText('');
});
chrome.runtime.onMessage.addListener(function(message) {
if (!message.options) {
return;
}
Object.keys(message.options).forEach(function(key) {
options[key] = message.options[key];
});
var opts = message.options;
if (opts.toolbar_icon) {
setIcon(opts.toolbar_icon);
}
});
chrome.storage.sync.get(defaults, function(items) {
options = items;
setIcon(items.toolbar_icon);
});