-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
142 lines (127 loc) · 3.71 KB
/
Copy pathcommon.js
File metadata and controls
142 lines (127 loc) · 3.71 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
136
137
138
139
140
141
142
// DEBUG flags
// 1 Functions
// 2 Database
// 4 Storage
// 8 Url
// 16 Filename
// 32 Gui
// 64 Downloading
//128 Container Parsing
function loadDatafromStorage() {
let data=browser.storage.local.get("AnticontainerData");
return data;
}
function loadDatafromOldExtension() {
var filename="./plugins/plugins.json";
fetch(filename)
.then(function(response) {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.text();
})
.then(function(response) {
let data=JSON.parse(response);
if(JSON.stringify(data) === '{}') {
console.log("No data to load at all...");
} else {
data=sortAndParseLoadedData(data);
saveDatatoStorage("AnticontainerData",data);
saveDatatoStorage("CachedData",data);
if (document.title=="Settings Page") {
buildHTMLOptionsList(data);
document.getElementById("ruleslist").selectedIndex=0;
// Update the form buttons
var event = new Event('change');
document.getElementById("ruleslist").dispatchEvent(event);
}
}
});
}
function sortAndParseLoadedData(dataToSort) {
let sortedData=dataToSort.sort(function(a, b) {
return a.prefix.localeCompare(b.prefix);
});
var ptr=0;
sortedData.forEach((entry) => {
entry.match=entry.match.replace(/http:/,"https?:");
if (entry.priority==null) {
entry.priority=1;
}
entry.ruleID=ptr;
ptr++;
});
return sortedData;
}
function saveDatatoStorage(Key,dataToSave) {
if ((typeof dataToSave != undefined) && (dataToSave != null)) {
let data=JSON.stringify(dataToSave);
browser.storage.local.set({[Key]: data})
.then(Success, onError);
}
function Success() {
}
function onError(error) {
console.log(error)
}
}
async function loadDatafromCache() {
var cachedData = await browser.storage.local.get("CachedData");
var tmp=[];
tmp=cachedData["CachedData"];
return tmp;
}
function buildHTMLOptionsList(data) {
var f=0;
const list=document.getElementById("ruleslist");
let tmp=data.sort(function(a, b) {
return a.prefix.localeCompare(b.prefix);
});
tmp.forEach( elem => {
// if ((elem.type == "resolver") || (elem.type == "redirector") || (elem.type == "sandbox")) {
if ((elem.type == "resolver") || (elem.type == "redirector")) {
var opt=document.getElementById("option_"+f);
if ( opt == null) {
var opt=document.createElement("option");
opt.id="option_"+f;
opt.textContent=elem.prefix;
opt.value=elem.ruleID;
list.appendChild(opt);
}
else {
opt.textContent=elem.prefix;
}
}
f++;
});
}
async function getPreference(name,defaultVal) {
let data = await browser.storage.local.get(name);
if(JSON.stringify(data) === '{}') {
data=defaultVal;}
else {
data=data[name];
}
return data;
}
function create_UUID(){
var dt = new Date().getTime();
var uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
var r = (dt + Math.random()*16)%16 | 0;
dt = Math.floor(dt/16);
return (c=="x" ? r :(r&0x3|0x8)).toString(16);
});
return uuid;
}
async function getDownloadWindowId(tmp) {
let id=-1;
var popupWindows = await browser.windows.getAll({windowTypes: ["popup"], populate: true });
if (popupWindows == null) {popupWindows = [];}
for (let f=0; f< popupWindows.length; f++) {
if (popupWindows[f].tabs[0].title == "Downloaded File History.") {
id=popupWindows[f].id;
}
}
return id;
}
export { loadDatafromOldExtension, loadDatafromStorage, loadDatafromCache, saveDatatoStorage, buildHTMLOptionsList, getPreference, create_UUID, getDownloadWindowId };