-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathuserjsUtils.sys.mjs
118 lines (103 loc) · 4.42 KB
/
userjsUtils.sys.mjs
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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { setTimeout } from "resource://gre/modules/Timer.sys.mjs";
import { FileUtils } from "resource://gre/modules/FileUtils.sys.mjs";
export const EXPORTED_SYMBOLS = ["userjsUtils"];
/**
* An object containing a list of user.js scripts with their corresponding URLs.
*
* @typedef {object} UserJsList
* @property {string[]} BetterfoxDefault - The URL for the BetterfoxDefault user.js script.
* @property {string[]} Securefox - The URL for the Securefox user.js script.
* @property {string[]} Fastfox - The URL for the Fastfox user.js script.
* @property {string[]} Peskyfox - The URL for the Peskyfox user.js script.
* @property {string[]} Smoothfox - The URL for the Smoothfox user.js script.
*/
/**
* An object containing a list of user.js scripts with their corresponding URLs.
*
* @type {UserJsList}
*/
export const userJsList = {
BetterfoxDefault: ["https://raw.githubusercontent.com/yokoffing/Betterfox/esr115/user.js"],
Securefox: ["https://raw.githubusercontent.com/yokoffing/Betterfox/120/Securefox.js"],
Fastfox: ["https://raw.githubusercontent.com/yokoffing/Betterfox/120/Fastfox.js"],
Peskyfox: ["https://raw.githubusercontent.com/yokoffing/Betterfox/120/Peskyfox.js"],
Smoothfox: ["https://raw.githubusercontent.com/yokoffing/Betterfox/120/Smoothfox.js"],
};
const PROFILE_DIR = Services.dirsvc.get("ProfD", Ci.nsIFile).path;
const userjs = PathUtils.join(PROFILE_DIR, "user.js");
export const userjsUtilsFunctions = {
userJsNameList() {
let list = [];
for (let name in userJsList) {
list.push(name);
}
},
/**
* Sets the user.js file with the contents of the file at the given URL.
*
* @async
* @param {string} url - The URL of the file to set as the user.js file.
* @returns {Promise<void>} - A Promise that resolves when the user.js file has been set.
*/
async setUserJSWithURL(url) {
try{userjs.remove(false);} catch(e) {}
fetch(url)
.then(response => response.text())
.then(async data => {
const PROFILE_DIR = Services.dirsvc.get("ProfD", Ci.nsIFile).path;
const userjs = PathUtils.join(PROFILE_DIR, "user.js");
const encoder = new TextEncoder("UTF-8");
const writeData = encoder.encode(data);
await IOUtils.write(userjs, writeData);
Services.obs.notifyObservers([], "floorp-restart-browser");
});
},
async getFileContent(url) {
const response = await fetch(url);
const text = await response.text();
return text;
},
async setUserJSWithName(name) {
const url = userJsList[name][0];
await userjsUtilsFunctions.setUserJSWithURL(url);
},
/**
* Resets preferences to Floorp's default values.
*
* @async
* @returns {Promise<void>}
*/
async resetPreferencesWithUserJsContents() {
const FileUtilsPath = FileUtils.getFile("ProfD", ["user.js"]);
const PROFILE_DIR = Services.dirsvc.get("ProfD", Ci.nsIFile).path;
if (!FileUtilsPath.exists()) {
console.warn("user.js does not exist");
const path = PathUtils.join(PROFILE_DIR, "user.js");
const encoder = new TextEncoder("UTF-8");
const data = encoder.encode("fake data");
await IOUtils.write(path, data);
}
const decoder = new TextDecoder("UTF-8");
const path = PathUtils.join(PROFILE_DIR, "user.js");
let read = await IOUtils.read(path);
let inputStream = decoder.decode(read);
const prefPattern = /user_pref\("([^"]+)",\s*(true|false|\d+|"[^"]*")\);/g;
let match;
while ((match = prefPattern.exec(inputStream)) !== null) {
if (!match[0].startsWith("//")) {
const settingName = match[1];
await new Promise(resolve => {
setTimeout(() => {
Services.prefs.clearUserPref(settingName);
console.info("resetting " + settingName);
}, 100);
resolve();
});
}
}
}
};