-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
74 lines (64 loc) · 2.13 KB
/
Copy pathbackground.js
File metadata and controls
74 lines (64 loc) · 2.13 KB
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
const OPEN_MODE_KEY = "openMode";
function openFullPageInTab() {
chrome.tabs.create({ url: chrome.runtime.getURL("fullpage.html") });
}
let tabClickListenerRegistered = false;
function registerTabClickListenerOnce() {
if (tabClickListenerRegistered) return;
tabClickListenerRegistered = true;
chrome.action.onClicked.addListener(openFullPageInTab);
}
/**
* setPopup tarayicida bazen asenkron tamamlanir; onClicked ancak popup
* gercekten ayarlandiktan sonra kaydedilmeli (aksi halde tiklamada sekme acilir).
*/
function applyOpenMode(mode, onApplied) {
const m = mode === "popup" ? "popup" : "tab";
const done = typeof onApplied === "function" ? onApplied : () => {};
if (m === "popup") {
chrome.action.setPopup({ popup: "popup.html" }, () => {
if (chrome.runtime.lastError) {
console.warn("DiffChecker setPopup(popup):", chrome.runtime.lastError.message);
}
done();
});
} else {
chrome.action.setPopup({ popup: "" }, () => {
if (chrome.runtime.lastError) {
console.warn("DiffChecker setPopup(clear):", chrome.runtime.lastError.message);
}
done();
});
}
}
function afterStorageReady(r) {
const mode = r[OPEN_MODE_KEY] !== undefined ? r[OPEN_MODE_KEY] : "tab";
applyOpenMode(mode, registerTabClickListenerOnce);
}
chrome.storage.local.get(OPEN_MODE_KEY, (r) => {
if (chrome.runtime.lastError) {
applyOpenMode("tab", registerTabClickListenerOnce);
return;
}
afterStorageReady(r);
});
chrome.storage.onChanged.addListener((changes, area) => {
if (area !== "local" || !changes.openMode) return;
applyOpenMode(changes.openMode.newValue || "tab");
});
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
if (msg && msg.type === "openModeChanged" && (msg.mode === "popup" || msg.mode === "tab")) {
applyOpenMode(msg.mode);
sendResponse({ ok: true });
}
return false;
});
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === "install") {
chrome.storage.local.get(OPEN_MODE_KEY, (r) => {
if (r[OPEN_MODE_KEY] === undefined) {
chrome.storage.local.set({ openMode: "tab" });
}
});
}
});