-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpopup.js
79 lines (76 loc) · 2.61 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
const defaultURL = {
url: '',
error: false,
};
const urlRegex = /^(https?|\*):\/\/.*\/.*$/;
var lastChangeTarget;
let fadeOut = (target) => setInterval(function () {
if (target.style.opacity > 0) {
target.style.opacity -= 0.1;
} else {
clearInterval(fadeEffect);
target.innerHTML = "";
}
}, 250);
chrome.storage.local.get(['requestThrottler'], (result) => {
const data = Object.assign(
{}, {
enabled: false,
urls: [{ ...defaultURL }],
defaultDelay: 2000,
},
result.requestThrottler
);
var app = new Vue({
el: '#app',
data: data,
methods: {
applyConfig: function() {
let newConfig = prompt('Please input new config (it should appear in JSON format as in example below), or leave as {} to reset.\n\n{"defaultDelay":"5000","enabled":true,"urls":[{"checked":true,"error":false,"url":"*://*/api/foo"},{"checked":true,"delay":"2000","error":false,"url":"https://joecoyle.net/api/bar"}]}\n', "{}");
if(newConfig) {
try {
chrome.storage.local.set({'requestThrottler': JSON.parse(newConfig)}, function() {
location.reload();
});
} catch {
alert("Error applying config. Is your JSON formatting correct?");
}
}
},
copyCurrentConfig: function() {
chrome.storage.local.get((config) => {
navigator.clipboard.writeText(JSON.stringify(config["requestThrottler"]));
lastChangeTarget = document.getElementById("messageDisplay");
lastChangeTarget.innerHTML = "Configuration copied to clipboard";
lastChangeTarget.style.opacity = 1;
fadeOut(lastChangeTarget);
});
},
addUrlInput: function() {
this.urls = this.urls.concat({ ...defaultURL });
},
removeUrlInput: function(index) {
this.urls.splice(index, 1);
},
updateStorage: function(newStorage) {
chrome.storage.local.set(newStorage);
}
},
updated: _.debounce(function() {
chrome.storage.local.get((currStorage) => {
var newStorage = {requestThrottler: {
defaultDelay: this.defaultDelay,
enabled: this.enabled,
urls: this.urls,
}};
if(JSON.stringify(currStorage) != JSON.stringify(newStorage)) {
this.updateStorage(newStorage);
lastChangeTarget = document.getElementById("messageDisplay");
lastChangeTarget.innerHTML = "Changes saved @ " + (new Date).toLocaleTimeString();
lastChangeTarget.style.opacity = 1;
fadeOut(lastChangeTarget);
}
});
}, 700)
});
});