-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup_script.js
76 lines (67 loc) · 1.9 KB
/
popup_script.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
//----------------------
$(function () {
chrome.storage.local.get(function(obj) {
if (("q" in obj) == false) { return }
let words = obj.q.split('+');
let text = "";
for (let i = 0; i < words.length; i++) {
let word = decodeURIComponent(words[i]);
word = escapeHtml(word);
text += word + " ";
}
$("#word").html(text);
if ("color" in obj) {
$("input[name='color']").val(obj.color);
}
if ("exact_match" in obj) {
if (obj.exact_match == "on") {
$("input[name='exact_match']").prop("checked", true);
}
}
if ("disable" in obj) {
if (obj.disable == "on") {
$("input[name='disable']").prop("checked", true);
}
}
});
$("#clear").on("click", function() {
clear();
});
$("#save").on("click", function() {
updateSettings();
});
});
function clear() {
chrome.storage.local.remove("q", null);
$("#word").html("");
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {type: "clear"}, function(res) {
});
});
}
function updateSettings() {
let obj = {};
obj.color = $("input[name='color']").val();
obj.exact_match = $("input[name='exact_match']:checked").val();
obj.disable = $("input[name='disable']:checked").val();
if (typeof obj.exact_match === "undefined") {
obj.exact_match = "off";
}
if (typeof obj.disable === "undefined") {
obj.disable = "off";
}
chrome.storage.local.set(obj, function(){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {type: "update"}, null);
});
});
}
function escapeHtml(str){
str = str.replace(/&/g, '&');
str = str.replace(/>/g, '>');
str = str.replace(/</g, '<');
str = str.replace(/"/g, '"');
str = str.replace(/'/g, ''');
str = str.replace(/`/g, '`');
return str;
}