-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground2.js
114 lines (107 loc) · 3.83 KB
/
background2.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
// import * as ScreenshotBackgroundWorker from "./background-only/screenshot.js"
import * as Utils from "./scripts/utils.js";
import * as ScreenshotFiles from "./scripts/screenshotStorage.js"
import * as UserSettingStorage from "./scripts/settingStorage.js";
// 每次重新執行都要檢查一次個人化設定
async function init() {
await UserSettingStorage.init()
}
init();
/**
* Click extension icon to open option page.
*/
chrome.action.onClicked.addListener(function (tab) {
if (chrome.runtime.openOptionsPage) chrome.runtime.openOptionsPage();
});
/**
* Monitor storage changes
* 1. Update option page when screenshot storage changes
*/
chrome.storage.onChanged.addListener((changes, namespace) => {
for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
console.log(`Storage key "${key}" in namespace "${namespace}" changed.`);
// console.log(`Storage key "${key}" in namespace "${namespace}" changed.`, `Old value was "${oldValue}", new value is "${newValue}".`);
}
});
/**
* Inject element when page update
*/
const host = "www.youtube.com";
const filter = { url: [{ hostEquals: host, pathEquals: "/watch" }, { hostEquals: host, pathEquals: "/live" }] };
function injectScript(details) {
chrome.scripting.executeScript({
target: { tabId: details.tabId, allFrames: false },
files: ["./scripts/content-scripts/inject-screenshot-button.js", "./scripts/content-scripts/inject-timeline-button.js"],
}).then(() => console.log("script injected"));
}
chrome.webNavigation.onCompleted.addListener(injectScript, filter);
chrome.webNavigation.onHistoryStateUpdated.addListener(injectScript, filter);
/**
* Shortcuts
*/
chrome.commands.onCommand.addListener(async (command, tab) => {
if (command === "youtube-screen-shot") {
if (!Utils.isYoutube(tab.url)) {
Utils.normalNotification(Utils._app_title, "截圖功能僅能在 Youtube 頁面上執行。", "");
return;
}
const response = await chrome.tabs.sendMessage(tab.id, { command: "screenshot" });
// console.log(response);
}
else if (command === "youtube-timeline") {
if (!Utils.isYoutube(tab.url)) {
Utils.normalNotification(Utils._app_title, "截圖功能僅能在 Youtube 頁面上執行。", "");
return;
}
const response = await chrome.tabs.sendMessage(tab.id, { command: "timeline" });
// console.log(response);
}
});
/**
* Monitor communication
*/
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
// console.log(request);
if (request.message === "setNotify") {
// NotifyAPI.Permission();
NotifyAPI.normalNotification(request.notification.title, request.notification.header, request.notification.subheader
);
sendResponse(true);
return true;
}
else if (request.message === "download") {
await ScreenshotFiles.download(request.payload);
sendResponse(true);
return true;
}
else if (request.message === "saveScreenshot") {
await ScreenshotFiles.set(request.payload);
sendResponse(true);
return true;
}
else if (request.message === "saveUserSetting") {
await UserSettingStorage.set(request.payload);
sendResponse(true);
return true;
}
// else if (request.message === "getUserSetting") {
// const setting = await UserSettingStorage.get();
// console.log(setting[UserSettingStorage.key], Date.now());
// sendResponse({ "result": setting[UserSettingStorage.key] });
// return true;
// }
else if (request.message === "delete") {
// console.log("start delete");
await ScreenshotFiles.remove(request.payload);
// console.log("end delete");
sendResponse(true);
return true;
}
else if (request.message === "youtubeEnded") {
// console.log("start delete");
await ScreenshotFiles.remove(request.payload);
// console.log("end delete");
sendResponse(true);
return true;
}
});