-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
51 lines (47 loc) · 1.06 KB
/
background.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
'use strict';
chrome.runtime.onMessage.addListener(({method, url}, {tab}) => {
if (method === 'is-canonical') {
chrome.pageAction.setIcon({
tabId: tab.id,
path: {
16: 'icons/gray/16.png',
32: 'icons/gray/32.png'
}
});
}
else if (method === 'offer-canonical') {
chrome.pageAction.setIcon({
tabId: tab.id,
path: {
16: 'icons/blue/16.png',
32: 'icons/blue/32.png'
}
});
}
chrome.pageAction.setTitle({
tabId: tab.id,
title: url
});
chrome.pageAction.show(tab.id);
});
chrome.pageAction.onClicked.addListener(tab => {
chrome.tabs.executeScript(tab.id, {
code: `{
location.replace(document.querySelector('link[rel="canonical"]').href);
}`
});
});
chrome.contextMenus.create({
id: 'copy-canonical',
title: 'Copy canonical link',
contexts: ["page_action"]
});
chrome.contextMenus.onClicked.addListener(({ menuItemId }, tab) => {
if (menuItemId === 'copy-canonical') {
chrome.tabs.executeScript(tab.id, {
code: `{
navigator.clipboard.writeText(document.querySelector('link[rel="canonical"]').href);
}`
});
}
});