-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.js
121 lines (103 loc) · 3.71 KB
/
options.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
// #region Globals
// Setting key constants
let SettingKeys = {};
// Mapping from URL to preset button ID (used to bold the matching button)
const urlToPresetButtonMap = {};
// #endregion Globals
// #region Event handlers
// Page load
document.addEventListener('DOMContentLoaded', async () =>
{
await populateSettingKeys();
await loadOptions();
// Add (and add click handlers to) pinned URL preset buttons
addPresetButton("btnPresetDefault", "Default", "chrome://newtab/");
addPresetButton("btnPresetBlankLight", "Blank Light", chrome.runtime.getURL("Resources/blankLight.html"));
addPresetButton("btnPresetBlankDark", "Blank Dark", chrome.runtime.getURL("Resources/blankDark.html"));
boldSelectedButton();
});
// Save button click
document.querySelector("#btnSave").addEventListener("click", saveOptions);
// User changing pinned URL field value (preset buttons don't fire this).
document.querySelector("#inptPinnedURL").addEventListener("input", boldSelectedButton);
// #endregion Event handlers
/**
* Grab our sync settings keys from the session storage (set by service_worker).
*/
async function populateSettingKeys()
{
const settings = await chrome.storage.session.get("SettingKeys");
SettingKeys = settings["SettingKeys"];
}
/**
* Load the user's settings from sync storage and populate the fields with them.
*/
async function loadOptions()
{
const settings = await chrome.storage.sync.get([SettingKeys.PinnedURL, SettingKeys.NoFocusPinnedTab]);
const pinnedURL = settings[SettingKeys.PinnedURL];
document.getElementById("inptPinnedURL").value = pinnedURL;
const noFocusPinnedTab = settings[SettingKeys.NoFocusPinnedTab];
document.getElementById("chkNoFocus").checked = noFocusPinnedTab;
}
/**
* Save the current settings to sync storage.
*/
function saveOptions()
{
const pinnedURL = document.getElementById("inptPinnedURL").value;
const noFocusPinnedTab = document.getElementById("chkNoFocus").checked;
chrome.storage.sync.set({
[SettingKeys.NoFocusPinnedTab]: noFocusPinnedTab,
[SettingKeys.PinnedURL]: pinnedURL
});
// Flash an indicator to let the user know we saved.
const status = document.getElementById("divStatus");
status.innerHTML = "Options Saved.";
setTimeout(() => { status.innerHTML = ""; }, 750);
}
/**
* Generate and add a preset button to the page, including its click handler.
* Also adds to urlToPresetButtonMap.
* @param {string} id ID for the new button
* @param {string} caption Caption for the new button
* @param {string} url Pinned URL that we should blow in when this button is clicked
*/
function addPresetButton(id, caption, url)
{
// Create the button
const button = document.createElement("button");
button.id = id;
button.innerText = caption;
button.classList.add("pinnedURLPresetButton");
// Add click handler
button.addEventListener("click", () =>
{
document.getElementById("inptPinnedURL").value = url;
boldSelectedButton();
});
// Add to presets container
document.querySelector("#spnPresetButtons").appendChild(button);
// Add URL-to-button-ID mapping
urlToPresetButtonMap[url] = id;
}
/**
* Bold the preset button (if any) that matches the current URL in the field.
*/
function boldSelectedButton() {
// Clear bold from all buttons
document.querySelectorAll("button.pinnedURLPresetButton").forEach(button => {
button.classList.remove("selectedButton");
});
// Bold the new matching button
const newURL = document.querySelector("#inptPinnedURL").value;
const buttonId = urlToPresetButtonMap[newURL];
if (buttonId)
{
const button = document.getElementById(buttonId);
if (button)
{
button.classList.add("selectedButton");
}
}
};