-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.js
More file actions
135 lines (121 loc) · 5.66 KB
/
options.js
File metadata and controls
135 lines (121 loc) · 5.66 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
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
130
131
132
133
134
135
const DEFAULT_PREFERENCES = {
commentsReadOnPageLoad: true,
hideFirstVisit: true,
highlightParagraphs: true,
highlightAdmin: true,
favoriteMe: true,
autoHideByDefault: false,
jumpToFirstUnread: false,
rewriteTwitterLinks: false,
rewriteTwitterDomain: "nitter.net",
forceOpenInNewTab: false,
colorParagraphs: "#84dc23",
colorComments: "#dc2328",
colorFavorites: "#23dcd7",
colorNavigation: "#7b23dc",
favoriteWords: [],
favoriteAuthors: [],
hiddenAuthors: [],
};
function saveOptions() {
const prefs = {
commentsReadOnPageLoad: document.getElementById("comments_read_on_page_load").checked,
hideFirstVisit: document.getElementById("hide_first_visit").checked,
highlightParagraphs: document.getElementById("highlight_paragraphs").checked,
highlightAdmin: document.getElementById("highlight_admin").checked,
favoriteMe: document.getElementById("favorite_me").checked,
autoHideByDefault: document.getElementById("auto_hide_default").checked,
jumpToFirstUnread: document.getElementById("jump_first_unread").checked,
rewriteTwitterLinks: document.getElementById("rewrite_twitter_links").checked,
rewriteTwitterDomain: document.getElementById("rewrite_twitter_domain").value || "nitter.net",
forceOpenInNewTab: document.getElementById("force_open_new_tab").checked,
colorParagraphs: document.getElementById("color_paragraphs").value,
colorComments: document.getElementById("color_comments").value,
colorFavorites: document.getElementById("color_favorites").value,
colorNavigation: document.getElementById("color_navigation").value,
favoriteWords: document.getElementById("favorite_words").value.split("\n"),
favoriteAuthors: document.getElementById("favorite_authors").value.split("\n"),
hiddenAuthors: document.getElementById("hidden_authors").value.split("\n"),
};
if (!CSS.supports("color", prefs.colorParagraphs)) {
prefs.colorParagraphs = defaultPreferences.colorParagraphs;
document.getElementById("color_paragraphs").value = prefs.colorParagraphs;
}
if (!CSS.supports("color", prefs.colorComments)) {
prefs.colorComments = defaultPreferences.colorComments;
document.getElementById("color_comments").value = prefs.colorComments;
}
if (!CSS.supports("color", prefs.colorFavorites)) {
prefs.colorFavorites = defaultPreferences.colorFavorites;
document.getElementById("color_favorites").value = prefs.colorFavorites;
}
if (!CSS.supports("color", prefs.colorNavigation)) {
prefs.colorNavigation = defaultPreferences.colorNavigation;
document.getElementById("color_navigation").value = prefs.colorNavigation;
}
prefs.favoriteWords = prefs.favoriteWords
.map((word) => word.trim().toLowerCase())
.filter((word) => word.length > 0);
prefs.favoriteAuthors = prefs.favoriteAuthors
.map((author) => author.trim().toLowerCase())
.filter((author) => author.length > 0);
prefs.hiddenAuthors = prefs.hiddenAuthors
.map((author) => author.trim().toLowerCase())
.filter((author) => author.length > 0);
updateUI(prefs);
writePreferences(prefs).then(() => {
document.getElementById("status_saved").style.display = "inline";
setTimeout(function () {
document.getElementById("status_saved").style.display = "none";
}, 2000);
});
window.close();
}
function updateUI(prefs) {
document.getElementById("comments_read_on_page_load").checked = prefs.commentsReadOnPageLoad;
document.getElementById("hide_first_visit").checked = prefs.hideFirstVisit;
document.getElementById("highlight_paragraphs").checked = prefs.highlightParagraphs;
document.getElementById("highlight_admin").checked = prefs.highlightAdmin;
document.getElementById("favorite_me").checked = prefs.favoriteMe;
document.getElementById("auto_hide_default").checked = prefs.autoHideByDefault;
document.getElementById("jump_first_unread").checked = prefs.jumpToFirstUnread;
document.getElementById("rewrite_twitter_links").checked = prefs.rewriteTwitterLinks;
document.getElementById("rewrite_twitter_domain").value = prefs.rewriteTwitterDomain || "";
document.getElementById("force_open_new_tab").checked = prefs.forceOpenInNewTab;
document.getElementById("color_paragraphs").value = prefs.colorParagraphs;
document.getElementById("color_comments").value = prefs.colorComments;
document.getElementById("color_favorites").value = prefs.colorFavorites;
document.getElementById("color_navigation").value = prefs.colorNavigation;
document.getElementById("favorite_words").value = prefs.favoriteWords?.join("\n") || "";
document.getElementById("favorite_authors").value = prefs.favoriteAuthors.join("\n");
document.getElementById("hidden_authors").value = prefs.hiddenAuthors.join("\n");
}
function restoreOptions() {
getPreference().then((prefs) => {
updateUI({...DEFAULT_PREFERENCES, ...prefs});
});
}
async function getPreference() {
// Firefox
if (typeof browser !== "undefined") {
return (await browser.storage.local.get("preferences"))["preferences"] || DEFAULT_PREFERENCES;
}
// Chromium based browsers
if (typeof chrome !== "undefined") {
return (await chrome.storage.local.get("preferences"))["preferences"] || DEFAULT_PREFERENCES;
}
throw new Error("No storage available");
}
async function writePreferences(value) {
// Firefox
if (typeof browser !== "undefined") {
return await browser.storage.local.set({ ["preferences"]: value });
}
// Chromium based browsers
if (typeof chrome !== "undefined") {
return await chrome.storage.local.set({ ["preferences"]: value });
}
throw new Error("No storage available");
}
document.addEventListener("DOMContentLoaded", restoreOptions);
document.getElementById("save").addEventListener("click", saveOptions);