-
Notifications
You must be signed in to change notification settings - Fork 26
/
popup.js
151 lines (116 loc) · 4.09 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {fastreadifyPage, patternsInclude, defaultHighlightSheet, defaultRestSheet, defaultAlgorithm} from './utils.js';
let applyButton = document.getElementById("applyButton");
let autoButton = document.getElementById("autoButton");
let excludePatternInput = document.getElementById("excludePattern");
let excludePageButton = document.getElementById("excludePageButton");
let restoreButton = document.getElementById("restore-button");
let highlightSheetInput = document.getElementById('highlight-input');
let restSheetInput = document.getElementById('rest-input');
let algorithmInput = document.getElementById('algorithmInput');
var buttonEnabledClass = 'button-enabled';
var buttonDisabledClass = 'button-disabled';
function setClass(element, cls){
element.className = cls;
}
async function updatePatternText(){
let [tab] = await chrome.tabs.query({active: true, currentWindow: true});
excludePatternInput.value = tab.url;
updateExcludeButtonText();
}
function updateExcludeButtonText(){
var currentPattern = excludePatternInput.value;
chrome.storage.sync.get(['excludedPatterns'], (data)=>{
if (data.excludedPatterns.indexOf(currentPattern) != -1){
excludePageButton.innerText = "Remove pattern from exclude list";
setClass(excludePageButton, buttonEnabledClass);
}
else{
excludePageButton.innerText = "Add pattern to exclude list";
setClass(excludePageButton, buttonDisabledClass);
}
});
}
updatePatternText();
function toggleExcludedPattern(pattern){
chrome.storage.sync.get(['excludedPatterns'], (data)=>{
var patterns = data.excludedPatterns;
var index = -1;
for (var i = 0; i < data.excludedPatterns.length; i++){
if (patterns[i] === pattern){
index = i;
break;
}
}
if (index === -1){
patterns.push(pattern);
}
else{
patterns.splice(index, 1);
}
chrome.storage.sync.set({'excludedPatterns': patterns});
updateExcludeButtonText();
});
}
excludePageButton.addEventListener("click", async () => {
toggleExcludedPattern(excludePatternInput.value);
});
function updateAutoApplyText(isAuto){
if (isAuto){
autoButton.innerText = 'Disable Auto Apply'
setClass(autoButton, buttonDisabledClass);
}
else{
autoButton.innerText = 'Enable Auto Apply'
setClass(autoButton, buttonEnabledClass);
}
}
chrome.storage.sync.get(['highlightSheet', 'restSheet', 'autoApply', 'algorithm'], (data) => {
highlightSheetInput.value = data.highlightSheet;
restSheetInput.value = data.restSheet;
algorithmInput.value = data.algorithm;
updateAutoApplyText(data.autoApply);
});
highlightSheetInput.addEventListener("input", async (text) => {
onHighlightInputChange();
});
restSheetInput.addEventListener("input", async (text) => {
onRestInputChange();
});
algorithmInput.addEventListener("input", async (text) => {
onAlgorithmInputChange();
});
excludePatternInput.addEventListener("input", async (text) => {
updateExcludeButtonText();
});
restoreButton.addEventListener("click", async () => {
chrome.storage.sync.set({'highlightSheet' : defaultHighlightSheet,
"restSheet": defaultRestSheet,
"algorithm": defaultAlgorithm
}
);
highlightSheetInput.value = defaultHighlightSheet;
restSheetInput.value = defaultRestSheet;
algorithmInput.value = defaultAlgorithm;
});
applyButton.addEventListener("click", async () => {
let [tab] = await chrome.tabs.query({active: true, currentWindow: true});
chrome.scripting.executeScript({
target: {tabId: tab.id},
function: fastreadifyPage,
});
});
autoButton.addEventListener("click", async () => {
chrome.storage.sync.get(['autoApply'], (data)=>{
updateAutoApplyText(!data.autoApply);
chrome.storage.sync.set({'autoApply': !data.autoApply});
});
});
function onHighlightInputChange(){
chrome.storage.sync.set({'highlightSheet': highlightSheetInput.value});
}
function onRestInputChange(){
chrome.storage.sync.set({'restSheet': restSheetInput.value});
}
function onAlgorithmInputChange(){
chrome.storage.sync.set({'algorithm': algorithmInput.value});
}