forked from ZachSaucier/Just-Read
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
559 lines (447 loc) · 18 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/* Use Ace https://ace.c9.io/ to create our CSS editor */
var editor = ace.edit("css-editor");
editor.setTheme("ace/theme/crimson_editor");
editor.getSession().setMode("ace/mode/css");
editor.$blockScrolling = Infinity;
// Enable zooming of the editor itself
editor.commands.addCommands([{
name: "increaseFontSize",
bindKey: "Ctrl-=|Ctrl-+",
exec: function(editor) {
var size = parseInt(editor.getFontSize(), 10) || 12;
editor.setFontSize(size + 1);
}
}, {
name: "decreaseFontSize",
bindKey: "Ctrl+-|Ctrl-_",
exec: function(editor) {
var size = parseInt(editor.getFontSize(), 10) || 12;
editor.setFontSize(Math.max(size - 1 || 1));
}
}, {
name: "resetFontSize",
bindKey: "Ctrl+0|Ctrl-Numpad0",
exec: function(editor) {
editor.setFontSize(12);
}
}, {
name: "save",
bindKey: "Ctrl+s",
exec: saveTheme
}]);
var changed = false,
stylesheetObj = {},
saveButton = document.getElementById("save"),
defaultLiItem,
defaultStylesheet = "default-styles.css";
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
// Given a chrome storage object, add them to our domain list
function setDomains(domains) {
var domainString = "";
for(var i = 0; i < domains.length; i++) {
domainString += domains[i] + "\n";
}
domainList.value = domainString;
}
// Given a chrome storage object, add them to our domain list
function getStylesFromStorage(storage) {
for(var key in storage) {
if(key === "auto-enable-site-list") {
setDomains(storage[key]);
} else if(key === "enable-pageCM") {
pageCM.checked = storage[key];
} else if(key === "enable-highlightCM") {
highlightCM.checked = storage[key];
} else if(key === "enable-linkCM") {
linkCM.checked = storage[key];
} else if(key === "show-del-btn") {
delMode.checked = storage[key];
} else if(key === "leave-pres") {
leavePres.checked = storage[key];
} else if(key === "fullscreen") {
fullScrn.checked = storage[key];
} else if(key.substring(0, 3) === "jr-") // Get the user's stylesheets
stylesheetObj[key.substring(3)] = storage[key];
}
}
// Set the chrome storage based on our stylesheet object
function setStylesOfStorage(nextFunc) {
for(var stylesheet in stylesheetObj) {
var obj = {};
obj['jr-' + stylesheet] = stylesheetObj[stylesheet];
chrome.storage.sync.set(obj, function() {
if(chrome.runtime.lastError
&& chrome.runtime.lastError.message === "QUOTA_BYTES_PER_ITEM quota exceeded") {
alert("File did not save: Your stylesheet is too big. Minifying it or removing lesser-used entries may help.\n\nYou can minify it at: https://cssminifier.com/");
} else {
nextFunc();
}
});
}
}
// Remove a given element from chrome storage
function removeStyleFromStorage(stylesheet) {
chrome.storage.sync.remove(stylesheet);
}
// Detect double click - pulled from http://stackoverflow.com/a/26296759/2065702
function makeDoubleClick(doubleClickCallback, singleClickCallback) {
return (function () {
var clicks = 0,
timeout;
return function () {
var me = this;
clicks++;
if (clicks == 1) {
singleClickCallback && singleClickCallback.apply(me, arguments);
timeout = setTimeout(function () {
clicks = 0;
}, 400);
} else {
timeout && clearTimeout(timeout);
doubleClickCallback && doubleClickCallback.apply(me, arguments);
clicks = 0;
}
};
}());
}
// Check to make sure there isn't a file with this name already. If so, add a number to the end
function checkFileName(fileName) {
var tempName = fileName,
count = 1;
while(stylesheetObj[tempName])
tempName = fileName.replace(/(\.[\w\d_-]+)$/i, "(" + count++ + ").css");
return tempName;
}
// Allow file names to be edited
function rename() {
var liItem = this;
// Hide the list item
liItem.style.display = "none";
// Insert an input temporarily
var fileNameInput = document.createElement("input");
fileNameInput.type = "text";
fileNameInput.value = fileNameInput.dataset.originalName = liItem.innerText;
// Update the style sheet object on blur
fileNameInput.onblur = function() {
// If the file name has changed
if(fileNameInput.value != fileNameInput.dataset.originalName) {
fileNameInput.value = checkFileName(fileNameInput.value);
stylesheetObj[fileNameInput.value] = stylesheetObj[liItem.innerText];
delete stylesheetObj[liItem.innerText];
removeStyleFromStorage("jr-" + liItem.innerText);
setTimeout(function() {
saveTheme();
}, 10);
var radio = document.createElement("input");
radio.type = "radio";
radio.name = "activeStylesheetRadios";
liItem.innerHTML = "";
liItem.appendChild(radio);
liItem.innerHTML += fileNameInput.value;
}
// Un-hide the list item
liItem.style.display = "list-item";
// Remove the input
fileNameInput.parentNode.removeChild(fileNameInput);
}
// Allow enter to be used to save the rename
fileNameInput.onkeyup = function(e) {
if(e.keyCode === 13)
fileNameInput.onblur();
}
if (liItem.nextSibling) {
liItem.parentNode.insertBefore(fileNameInput, liItem.nextSibling);
}
else {
liItem.parentNode.appendChild(fileNameInput);
}
fileNameInput.focus();
}
// Make sure the user wants to change files before saving
function confirmChange() {
if(changed)
if (window.confirm("Do you really want to change files before saving?"))
return false;
else
return true;
else
return false;
}
function styleListOnClick() {
// Don't do anything if it's already active
if(!this.classList.contains("active")) {
var cancel = confirmChange();
if(!cancel) {
// Switch out current CSS for the selected one
var fileName = this.textContent;
// Open up the file from localStorage
editor.setValue(stylesheetObj[fileName] === undefined ? "" : stylesheetObj[fileName], -1);
// Toggle the active class on the list items
if(document.querySelector(".stylesheets .active"))
document.querySelector(".stylesheets .active").classList.remove("active");
this.classList.add("active");
localStorage.currentTheme = fileName;
changed = false;
}
}
};
// The stuff to fire after the stylesheets have been loaded
function continueLoading() {
// Get the currently used stylesheet
chrome.storage.sync.get('currentTheme', function(result) {
var currTheme = result.currentTheme || defaultStylesheet;
// Based on that object, populate the list values
var list = document.querySelector(".stylesheets"),
count = 0;
for (var stylesheet in stylesheetObj) {
var li = document.createElement("li"),
liClassList = li.classList,
radio = document.createElement("input");
radio.type = "radio";
radio.name = "activeStylesheetRadios";
// If the sheet is the one currently applied, add a signifier class
if(stylesheet === currTheme) {
liClassList.add("used");
radio.setAttribute("checked", true);
}
// Add them to the li element
li.appendChild(radio);
li.innerHTML += stylesheet;
// Lock the default-styles.css file (prevent deletion)
if(stylesheet === defaultStylesheet) {
defaultLiItem = li;
liClassList.add("locked");
}
// Make the first one active
if(count === 0) {
liClassList.add("active");
var fileName = li.textContent;
editor.setValue(stylesheetObj[fileName] === undefined ? "" : stylesheetObj[fileName], -1);
}
list.appendChild(li);
count++;
}
stylesheetListItems = document.querySelectorAll(".stylesheets li");
[].forEach.call(stylesheetListItems, function(item, i) {
if(!item.classList.contains("locked"))
item.onclick = makeDoubleClick(rename, styleListOnClick);
else // Prevent the locked items from being changed in name
item.onclick = styleListOnClick;
});
// Add the listener for the save animation
saveButton.addEventListener("animationend", function() {
saveButton.classList.remove("saved");
});
saveButton.addEventListener("webkitAnimationEnd", function() {
saveButton.classList.remove("saved");
});
});
// Keep track of changes since last save
editor.on('change', function() {
if (editor.curOp && editor.curOp.command.name)
changed = true;
});
}
// Obtain the stylesheet strings from localStorage and put them in our stylesheet object
function getStylesheets() {
chrome.storage.sync.get(null, function (result) {
// Collect all of our stylesheets in our object
getStylesFromStorage(result);
if(isEmpty(stylesheetObj)) { // Not found, so we add our default
// Open the default CSS file and save it to our object
var xhr = new XMLHttpRequest();
xhr.open('GET', chrome.extension.getURL(defaultStylesheet), true);
xhr.onreadystatechange = function() {
if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
// Save the file's contents to our object
stylesheetObj[defaultStylesheet] = xhr.responseText;
// Save it to Chrome storage
setStylesOfStorage();
continueLoading();
}
}
xhr.send();
return;
} else {
continueLoading();
}
});
}
function continueSaving() {
var currFileElem = document.querySelector(".stylesheets .active");
if(currFileElem.classList.contains("locked")) { // The file is locked, so make a new one with the same name
var fileName = checkFileName(currFileElem.innerText);
// Add a new list element
var list = document.querySelector(".stylesheets"),
li = document.createElement("li"),
radio = document.createElement("input");
radio.type = "radio";
radio.name = "activeStylesheetRadios";
li.appendChild(radio);
li.innerHTML += fileName;
// Make it active
if(document.querySelector(".stylesheets .active"))
document.querySelector(".stylesheets .active").classList.remove("active");
li.classList.add("active");
// Force them to save to keep it
changed = true;
list.appendChild(li);
document.querySelector(".stylesheets").lastChild.onclick = makeDoubleClick(rename, styleListOnClick);
// Update our stylesheet storage
useTheme();
}
// Show the save animation
saveButton.classList.add("saved");
// Note that the file has been saved
changed = false;
}
function saveTheme() {
// Get the name of the current file being edited
var currFileElem = document.querySelector(".stylesheets .active");
// Save that file to localStorage
stylesheetObj[currFileElem.innerText] = editor.getValue();
setStylesOfStorage(continueSaving);
}
function useTheme() {
var themeToUse = document.querySelector(".stylesheets .active"),
previouslyUsed = document.querySelector(".stylesheets .used");
// Save the current theme
if(!themeToUse.classList.contains("locked"))
saveTheme();
// Remove the used class from the old list item
if(previouslyUsed !== null)
previouslyUsed.classList.remove("used");
// Update the class to show it's applied
themeToUse.classList.add("used");
// Make this radio button active
themeToUse.querySelector("input").checked = true;
// Apply the current theme
var sheet = themeToUse.innerText;
chrome.storage.sync.set({"currentTheme": sheet});
// Tell that we changed it
useButton.classList.add("used");
// Add the listener for the use animation
useButton.addEventListener("animationend", function() {
useButton.classList.remove("used");
});
useButton.addEventListener("webkitAnimationEnd", function() {
useButton.classList.remove("used");
});
}
var newFileInput = document.getElementById("new-file"),
addButton = document.getElementById("add"),
saveButton = document.getElementById("save"),
useButton = document.getElementById("use"),
removeButton = document.getElementById("remove"),
stylesheetListItems,
domainList = document.getElementById("domainList");
getStylesheets();
// Update the domain list with any new values
domainList.onkeyup = function(e) {
var domainLine = domainList.value.split("\n").filter(String);
chrome.storage.sync.set({"auto-enable-site-list": domainLine});
}
// Allow the "Enter" key to be used to add new stylesheets
newFileInput.onkeyup = function(e) {
if(e.keyCode === 13)
add.onclick();
}
// Create a new file with the given name
add.onclick = function() {
if(newFileInput.value !== "") {
var fileName = newFileInput.value;
// If it has .css at the end, remove it
if(fileName.slice(-4) === ".css")
fileName = fileName.slice(0, -4);
// Parse out the other stuff we don't want and add .css
fileName = fileName.replace(/[^a-z0-9]/gi, '-').toLowerCase() + ".css";
// If there's already a file with this name, change it
fileName = checkFileName(fileName);
// Add a new list element
var list = document.querySelector(".stylesheets"),
li = document.createElement("li");
li.innerHTML = '<input type="radio" name="activeStylesheetRadios">' + fileName;
// Make it active
if(document.querySelector(".stylesheets .active"))
document.querySelector(".stylesheets .active").classList.remove("active");
li.classList.add("active");
// Clear out the editor and add some smart defaults
editor.setValue("/* Some defaults you may want */\n.simple-container {\n max-width: 600px;\n margin: 0 auto;\n padding-top: 70px;\n padding-bottom: 20px;\n}\nimg { max-width: 100%; }\n/* Also keep in mind that the close button is by default black. */\n\n\n", -1);
// Force them to save to keep it
changed = true;
list.appendChild(li);
document.querySelector(".stylesheets").lastChild.onclick = makeDoubleClick(rename, styleListOnClick);
// Update our stylesheet storage
saveTheme();
newFileInput.value = "";
}
}
// Save the current code to the current file
saveButton.onclick = saveTheme;
// Use the selected stylesheet
useButton.onclick = useTheme;
// Remove the selected file
removeButton.onclick = function() {
// Select the item to be removed
var elem = document.querySelector(".stylesheets .active");
// Make sure they can't delete locked files
if(!elem.classList.contains("locked")) {
// Add confimation
if (window.confirm("Do you really want to remove this file?")) {
// Remove the file from our object
delete stylesheetObj[document.querySelector(".stylesheets .active").innerText];
// Remove the file from Chrome's storage
removeStyleFromStorage("jr-" + elem.innerText);
// Check to see if it's the currently used sheet - if so set it to the default
if(elem.classList.contains("used")) {
elem.classList.remove("active");
chrome.storage.sync.set({"currentTheme": defaultStylesheet}, function() {
defaultLiItem.querySelector("input").checked = true;
styleListOnClick.call(defaultLiItem);
defaultLiItem.classList.add("used", "active");
});
}
// Remove it from the list
elem.parentNode.removeChild(elem);
editor.setValue("", -1);
}
} else
alert("This file is locked and cannot be deleted.");
// Otherwise we do nothing
}
var pageCM = document.getElementById("pageCM"),
highlightCM = document.getElementById("highlightCM"),
linkCM = document.getElementById("linkCM"),
fullScrn = document.getElementById("fullScrn");
pageCM.onchange = function() {
chrome.storage.sync.set({"enable-pageCM": this.checked});
chrome.runtime.sendMessage({updateCMs: "true"});
}
highlightCM.onchange = function() {
chrome.storage.sync.set({"enable-highlightCM": this.checked});
chrome.runtime.sendMessage({updateCMs: "true"});
}
linkCM.onchange = function() {
chrome.storage.sync.set({"enable-linkCM": this.checked});
chrome.runtime.sendMessage({updateCMs: "true"});
}
fullScrn.onchange = function() {
chrome.storage.sync.set({"fullscreen": this.checked});
chrome.runtime.sendMessage({fullscreen: "true"});
}
var addOptBtn = document.getElementById("additional"),
closeAddBtn = document.getElementById("closeAdditional"),
addOpt = document.querySelector(".additional-options");
addOptBtn.onclick = closeAddBtn.onclick = function() {
addOpt.classList.toggle("hidden");
}
var delMode = document.getElementById("delModeBtn"),
leavePres = document.getElementById("leavePres");
delMode.onchange = function() {
chrome.storage.sync.set({"show-del-btn": this.checked});
}
leavePres.onchange = function() {
chrome.storage.sync.set({"leave-pres": this.checked});
}