-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
129 lines (109 loc) · 3.54 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"use strict";
import { MessageSet, CMIDSet, CommandSet } from "./js/dataSet.js";
import { Function } from "./js/function.js";
chrome.runtime.onInstalled.addListener(() => {
// 先移除全部的 contextMenus。
chrome.contextMenus.removeAll();
// 建立 contextMenus。
chrome.contextMenus.create({
title: chrome.i18n.getMessage("stringPinSelectedContent"),
contexts: ["selection"],
documentUrlPatterns: [
"*://*.youtube.com/*",
],
id: CMIDSet.PinSelectedContent,
});
chrome.contextMenus.create({
title: chrome.i18n.getMessage("stringAppendPinSelectedContent"),
contexts: ["selection"],
documentUrlPatterns: [
"*://*.youtube.com/*",
],
id: CMIDSet.AppendPinSelectedContent,
});
chrome.contextMenus.create({
title: chrome.i18n.getMessage("stringUnpinSelectedContent"),
contexts: ["page"],
documentUrlPatterns: [
"*://*.youtube.com/*",
],
id: CMIDSet.UnpinSelectedContent,
});
chrome.contextMenus.create({
title: chrome.i18n.getMessage("stringResetPinnedContentPosition"),
contexts: ["page"],
documentUrlPatterns: [
"*://*.youtube.com/*",
],
id: CMIDSet.ResetPinnedContentPosition,
});
chrome.contextMenus.create({
title: chrome.i18n.getMessage("stringTogglePinnedContent"),
contexts: ["page"],
documentUrlPatterns: [
"*://*.youtube.com/*",
],
id: CMIDSet.TogglePinnedContent,
});
});
chrome.contextMenus.onClicked.addListener((info, _tab) => {
const command = Function.getCommand(info.menuItemId);
if (command === undefined) {
Function.writeConsoleLog(info.menuItemId);
return;
}
Function.sendMessageToTab(command, true)
.catch((error) => {
Function.writeConsoleLog(error);
});
});
chrome.commands.onCommand.addListener(command => {
switch (command) {
case CommandSet.PinSelectedContent:
case CommandSet.AppendPinSelectedContent:
case CommandSet.UnpinSelectedContent:
case CommandSet.ResetPinnedContentPosition:
case CommandSet.TogglePinnedContent:
Function.sendMessageToTab(command, true)
.catch(error => {
Function.writeConsoleLog(error);
});
break;
default:
Function.writeConsoleLog(command);
break;
}
});
chrome.runtime.onMessage.addListener((message, _sender, _sendResponse) => {
if (message === MessageSet.WakeUp) {
updateExtensionApperance();
} else {
Function.writeConsoleLog(message);
}
});
/**
* 更新擴充功能的外觀
*/
function updateExtensionApperance() {
// 更新 contextMenus 的標題。
Function.updateContextMenusTitle(
CMIDSet.PinSelectedContent,
chrome.i18n.getMessage("stringPinSelectedContent")
);
Function.updateContextMenusTitle(
CMIDSet.AppendPinSelectedContent,
chrome.i18n.getMessage("stringAppendPinSelectedContent")
);
Function.updateContextMenusTitle(
CMIDSet.UnpinSelectedContent,
chrome.i18n.getMessage("stringUnpinSelectedContent")
);
Function.updateContextMenusTitle(
CMIDSet.ResetPinnedContentPosition,
chrome.i18n.getMessage("stringResetPinnedContentPosition")
);
Function.updateContextMenusTitle(
CMIDSet.TogglePinnedContent,
chrome.i18n.getMessage("stringTogglePinnedContent")
);
}