-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
112 lines (95 loc) · 3.5 KB
/
popup.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
const warning = document.getElementById("warning");
const customTitleInput = document.getElementById("customTitleInput");
const setTitleButton = document.getElementById("setTitleButton");
const resetTitleButton = document.getElementById("resetTitleButton");
const maintainCheckbox = document.getElementById("maintainCheckbox");
const configureShortcutsButton = document.getElementById(
"configureShortcutsButton",
);
const debugModeCheckbox = document.getElementById("debugModeCheckbox");
function log(...args) {
console.log("%c[Popup]", "color: fuchsia", ...args);
}
function isRestrictedPage(url) {
const restrictedPages = [
/^chrome:\/\//,
/^edge:\/\//,
/^about:/,
/^view-source:/,
/^data:/,
/chromewebstore.google.com/,
];
return restrictedPages.some((pattern) => url.match(pattern));
}
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tabId = tabs[0].id;
chrome.runtime.sendMessage({ action: "getState", tabId }, (response) => {
console.log("Received response", response);
if (response) {
customTitleInput.value = response.customTitle ?? "";
maintainCheckbox.checked = response.maintain ?? true;
debugModeCheckbox.checked = response.debugMode ?? false;
}
// Setting it here so it doesn't flash from this to the value on load
customTitleInput.placeholder = "Enter custom title";
});
if (isRestrictedPage(tabs[0].url)) {
warning.style.display = "block";
// Don't disable any inputs, just in case the user is planning on
// setting them for subsequent navigations in this tab
}
});
function setCustomTitleData(customTitle) {
if (customTitleInput.value?.trim() === "") {
// To reflect what it is converted to by the background script
customTitleInput.value = "\u200B";
}
console.log("Sending setCustomTitle message to background");
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tabId = tabs[0].id;
chrome.runtime.sendMessage({
action: "setCustomTitle",
customTitle,
tabId,
});
});
}
customTitleInput.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
setCustomTitleData(customTitleInput.value);
}
});
setTitleButton.addEventListener("click", () => {
setCustomTitleData(customTitleInput.value);
});
resetTitleButton.addEventListener("click", () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tabId = tabs[0].id;
console.log("Sending resetCustomTitle message to background");
chrome.runtime.sendMessage({
action: "resetCustomTitle",
tabId,
});
customTitleInput.value = "";
});
});
maintainCheckbox.addEventListener("change", (event) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tabId = tabs[0].id;
console.log("Sending updateMaintain message to background");
chrome.runtime.sendMessage({
action: "updateMaintain",
maintain: event.target.checked,
tabId,
});
});
});
configureShortcutsButton.addEventListener("click", () => {
chrome.tabs.create({ url: "chrome://extensions/shortcuts" });
});
debugModeCheckbox.addEventListener("change", (event) => {
chrome.runtime.sendMessage({
action: "changeDebugMode",
debugMode: event.target.checked,
});
});