-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbackground.js
103 lines (96 loc) · 2.34 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
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
const CONTEXT_MENU_CONTENTS = {
/** @type {chrome.contextMenus.CreateProperties[]} */
link: [
{
title: 'Open in Freedium',
type: 'normal',
id: 'freedium-link',
targetUrlPatterns: [
'*://*.medium.com/*',
'*://medium.com/*',
],
},
],
/** @type {chrome.contextMenus.CreateProperties[]} */
page: [
{
title: 'Open in Freedium',
type: 'normal',
id: 'freedium-page',
documentUrlPatterns: [
'*://*.medium.com/*',
'*://medium.com/*',
],
},
],
}
const setUpContextMenus = () => {
chrome.storage.sync.get(
{ patterns: '' },
(items) => {
/** @type {string} */
const patterns = items.patterns;
const patternsArray = patterns.replace(/\r/g, '').split('\n').filter(p => p).map(p => p.trim());
CONTEXT_MENU_CONTENTS.link.forEach((command) => {
chrome.contextMenus.create({
title: command.title,
type: command.type,
id: command.id,
targetUrlPatterns: command.targetUrlPatterns.concat(patternsArray),
contexts: ['link'],
});
});
CONTEXT_MENU_CONTENTS.page.forEach((command) => {
chrome.contextMenus.create({
title: command.title,
type: command.type,
id: command.id,
documentUrlPatterns: command.documentUrlPatterns.concat(patternsArray),
contexts: ['page'],
});
});
}
);
};
/**
* Open a URL in Freedium
* @param {string} url
* @param {boolean} newTab - open in a new tab?
* @returns
*/
const openInFreedium = (url, newTab) => {
if (!url) {
return;
}
if (newTab) {
chrome.tabs.create({
url: 'https://freedium.cfd/' + url,
})
} else {
chrome.tabs.update({
url: 'https://freedium.cfd/' + url,
})
}
};
chrome.runtime.onInstalled.addListener(() => {
setUpContextMenus();
});
chrome.runtime.onMessage.addListener(
(request, sender, sendResponse) => {
if (request.message === "settingsSaved") {
chrome.contextMenus.removeAll(() => {
setUpContextMenus();
});
}
}
);
chrome.contextMenus.onClicked.addListener((item) => {
switch (item.menuItemId) {
case 'freedium-link':
openInFreedium(item.linkUrl, true);
break;
case 'freedium-page':
openInFreedium(item.pageUrl, false);
break;
}
});