Skip to content

Commit f725f85

Browse files
committed
wip
1 parent 76e4ccd commit f725f85

File tree

12 files changed

+153
-174
lines changed

12 files changed

+153
-174
lines changed

empty_script.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ export default {
1111
blackList: [],
1212
whiteList: [],
1313

14-
// run (if enable autorun) in web page context
15-
onDocumentStart: () => {},
16-
onDocumentEnd: () => {},
17-
onDocumentIdle: () => {},
14+
// path to script file that run (if enable autorun) in web page context
15+
onDocumentStart: "",
16+
onDocumentEnd: "",
17+
onDocumentIdle: "",
1818

1919
// run onclick in extension-popup-page context
2020
onClickExtension: () => {},

popup/helpers/lang.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@ export const LANG = {
77

88
let currentLangKey = null;
99

10-
export async function setLang(lang) {
10+
export function setLang(lang) {
1111
if (lang in LANG) {
1212
currentLangKey = lang;
13-
await langSaver.set(lang);
13+
langSaver.set(lang);
1414
} else {
1515
alert("WRONG LANG KEY " + lang);
1616
}
1717
}
1818

19-
export async function toggleLang() {
19+
export function toggleLang() {
2020
let newLang = currentLangKey === LANG.vi ? LANG.en : LANG.vi;
2121
currentLangKey = newLang;
22-
await setLang(newLang);
22+
setLang(newLang);
2323
return newLang;
2424
}
2525

26-
export async function getLang() {
27-
if (!currentLangKey) currentLangKey = await langSaver.get(LANG.vi);
26+
export function getLang() {
27+
if (!currentLangKey) currentLangKey = langSaver.get(LANG.vi);
2828
return currentLangKey;
2929
}
3030

31-
export async function getFlag() {
32-
return "./assets/flag-" + (await getLang()) + ".png";
31+
export function getFlag() {
32+
return "./assets/flag-" + getLang() + ".png";
3333
}
3434

3535
export function t(o) {

popup/helpers/storage.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,50 @@
11
import { allScripts } from "../../scripts/index.js";
2-
import { localStorage } from "../../scripts/helpers/utils.js";
32

43
const createVariableSaver = (key, defaultValue = null) => ({
5-
set: async (data) => {
6-
await localStorage.set(key, data);
4+
set: (data) => {
5+
localStorage.setItem(key, JSON.stringify(data));
76
},
8-
get: async (_defaultValue) => {
9-
return await localStorage.get(key, _defaultValue || defaultValue);
7+
get: (_defaultValue) => {
8+
return (
9+
JSON.parse(localStorage.getItem(key) || "null") ??
10+
_defaultValue ??
11+
defaultValue
12+
);
1013
},
1114
});
1215

1316
const createScriptsSaver = (key, addToHead = true) => {
14-
const getIds = async () =>
15-
(await localStorage.get(key, [])).filter(
17+
const getIds = () =>
18+
JSON.parse(localStorage.getItem(key) ?? "[]").filter(
1619
(savedScriptId) => savedScriptId in allScripts
1720
);
18-
const has = async (script) => {
19-
let current = await getIds();
21+
const has = (script) => {
22+
let current = getIds();
2023
let exist = current.findIndex((id) => id === script.id) >= 0;
2124
return exist;
2225
};
23-
const add = async (script) => {
24-
let current = await getIds();
26+
const add = (script) => {
27+
let current = getIds();
2528
let newList = current.filter((id) => id != script.id); // remove duplicate
2629
if (addToHead) newList.unshift(script.id); // only save script id
2730
else newList.push(script.id);
28-
await localStorage.set(key, newList);
31+
localStorage.setItem(key, JSON.stringify(newList));
2932
};
30-
const remove = async (script) => {
31-
let current = await getIds();
33+
const remove = (script) => {
34+
let current = getIds();
3235
let newList = current.filter((id) => id !== script.id);
33-
await localStorage.set(key, newList);
36+
localStorage.setItem(key, JSON.stringify(newList));
3437
console.log("removed ", script);
3538
};
36-
const toggle = async (script) => {
37-
let exist = await has(script);
38-
if (exist) await remove(script);
39-
else await add(script);
39+
const toggle = (script) => {
40+
let exist = has(script);
41+
if (exist) remove(script);
42+
else add(script);
4043
};
41-
const clear = async () => {
42-
await localStorage.set(key, []);
44+
const clear = () => {
45+
localStorage.setItem(key, "[]");
4346
};
44-
const get = async () =>
45-
(await getIds()).map((savedScriptId) => allScripts[savedScriptId]);
47+
const get = () => getIds().map((savedScriptId) => allScripts[savedScriptId]);
4648

4749
return { add, remove, has, toggle, clear, getIds, get };
4850
};

popup/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ const searchInput = document.querySelector(".search input");
3434
const searchFound = document.querySelector(".search .searchFound");
3535

3636
async function initLanguage() {
37-
flagImg.setAttribute("src", await getFlag());
37+
flagImg.setAttribute("src", getFlag());
3838

3939
flagImg.onclick = async () => {
40-
await toggleLang();
41-
flagImg.setAttribute("src", await getFlag());
40+
toggleLang();
41+
flagImg.setAttribute("src", getFlag());
4242

4343
// reset UI
4444
createTabs();
@@ -246,7 +246,8 @@ function createScriptButton(script, isFavorite = false) {
246246
addFavoriteBtn.onclick = (e) => {
247247
e.stopPropagation();
248248
e.preventDefault();
249-
favoriteScriptsSaver.toggle(script).then(createTabs);
249+
favoriteScriptsSaver.toggle(script);
250+
createTabs();
250251
};
251252
button.appendChild(addFavoriteBtn);
252253

scripts/background-scripts/index.js

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,60 @@
11
// import { allScripts } from "../index.js";
2-
// import { Events } from "../helpers/constants.js";
3-
// import { MsgType } from "../helpers/constants.js";
4-
// import {
5-
// checkBlackWhiteList,
6-
// isActiveScript,
7-
// isEmptyFunction,
8-
// isFunction,
9-
// runScriptInTab,
10-
// } from "../helpers/utils.js";
11-
12-
// const CACHED = {
13-
// runCount: {},
14-
// };
15-
16-
// chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
17-
// console.log("> Received message:", message, sender?.tab?.url);
18-
19-
// switch (message.type) {
20-
// case MsgType.runScript:
21-
// runScript(message.event, sender.tab);
22-
// break;
23-
// }
24-
// });
25-
26-
// async function runScript(event, tab) {
27-
// let funcName = event,
28-
// tabId = tab.id,
29-
// url = tab.url;
30-
31-
// if (!(tabId in CACHED.runCount) || event === Events.onDocumentStart)
32-
// CACHED.runCount[tabId] = 0;
33-
34-
// for (let script of Object.values(allScripts)) {
35-
// try {
36-
// if (!checkBlackWhiteList(script, url)) continue;
37-
38-
// let func = script[funcName];
39-
// if (isFunction(func) && !isEmptyFunction(func)) {
40-
// let isActive = (await isActiveScript(script.id)) ?? true;
41-
// if (isActive) {
42-
// runScriptInTab({ func, tabId });
43-
// console.log(
44-
// `%c > Run ${script.id} ${funcName} in ${url}`,
45-
// "background: #222; color: #bada55"
46-
// );
47-
// CACHED.runCount[tabId]++;
48-
// }
49-
// }
50-
// } catch (e) {
51-
// console.log("ERROR at script " + script?.id, e);
52-
// }
2+
// import { Events, EventMap } from "../helpers/constants.js";
3+
4+
// chrome.scripting.registerContentScripts(
5+
// [
6+
// {
7+
// id: "ufs_global_webpage_context",
8+
// allFrames: true,
9+
// matches: ["<all_urls>"],
10+
// js: ["scripts/content-scripts/scripts/ufs_global_webpage_context.js"],
11+
// runAt: "document_start",
12+
// world: chrome.scripting.ExecutionWorld.MAIN,
13+
// },
14+
// ],
15+
// () => {
16+
// console.log("Register content script DONE.");
5317
// }
18+
// );
19+
20+
// (() => {
21+
// let scripts = Object.values(allScripts)
22+
// .map((s) =>
23+
// Object.values(Events).map((e) => {
24+
// if (!e in s) return null;
25+
// let isString = typeof s[e] === "string";
26+
// let isArray = Array.isArray(s[e]);
27+
// if (!(isString || isArray)) return null;
28+
29+
// let js = isString ? [getscriptURL(s[e])] : s[e].map(getscriptURL);
30+
31+
// // add global helper to head of array
32+
// js.unshift(
33+
// getscriptURL("content-scripts/scripts/ufs_global_webpage_context.js")
34+
// );
35+
// return {
36+
// id: s.id + "-" + e,
37+
// allFrames: true,
38+
// matches: s.whiteList || ["<all_urls>"],
39+
// excludeMatches: s.blackList || [],
40+
// js: js,
41+
// runAt: EventMap[e],
42+
// world: chrome.scripting.ExecutionWorld.MAIN,
43+
// };
44+
// })
45+
// )
46+
// .flat()
47+
// .filter((_) => _);
48+
49+
// console.log(scripts);
50+
51+
// chrome.scripting.registerContentScripts(scripts, () => {
52+
// console.log("Register content script DONE.");
53+
// });
54+
// })();
5455

55-
// updateBadge(tabId, CACHED.runCount[tabId]);
56+
// function getscriptURL(fileName) {
57+
// return "scripts/" + fileName;
5658
// }
5759

5860
// function updateBadge(tabId, text = "", bgColor = "#666") {

scripts/content-scripts/document_end.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
(async () => {
2-
let key = "activeScripts";
3-
let ids = (await chrome.storage.sync.get([key]))?.[key] || "";
2+
let ids = localStorage.getItem("activeScripts") || "";
43
window.dispatchEvent(
54
new CustomEvent("ufs-run-page-scripts", {
65
detail: {

scripts/content-scripts/document_idle.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
(async () => {
2-
let key = "activeScripts";
3-
let ids = (await chrome.storage.sync.get([key]))?.[key] || "";
2+
let ids = localStorage.getItem("activeScripts") || "";
43
window.dispatchEvent(
54
new CustomEvent("ufs-run-page-scripts", {
65
detail: {

scripts/content-scripts/document_start.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@
3434
"/scripts/content-scripts/scripts/ufs_global_webpage_context.js"
3535
)
3636
);
37-
38-
let key = "activeScripts";
39-
let ids = (await chrome.storage.sync.get([key]))?.[key] || "";
37+
let ids = localStorage.getItem("activeScripts") || "";
38+
let path = chrome.runtime.getURL("/scripts/");
4039
let search = new URLSearchParams({
4140
ids: ids,
41+
path: path,
4242
event: "onDocumentStart",
43-
path: chrome.runtime.getURL("/scripts/"),
4443
}).toString();
4544

4645
injectScript(

scripts/fb_revealDeletedMessages.js

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,39 @@ export default {
1111
whiteList: ["https://*.facebook.com/*", "https://*.messenger.com/*"],
1212

1313
onDocumentStart: () => {
14-
// const WebSocketOrig = window.WebSocket;
15-
// window.WebSocket = function fakeConstructor(dt, config) {
16-
// const websocket_instant = new WebSocketOrig(dt, config);
17-
// websocket_instant.addEventListener("message", async function (achunk) {
18-
// const utf8_str = new TextDecoder("utf-8").decode(achunk.data);
19-
// // Do something here
20-
// console.log(utf8_str);
21-
// });
22-
// return websocket_instant;
23-
// };
24-
// window.WebSocket.prototype = WebSocketOrig.prototype;
25-
// window.WebSocket.prototype.constructor = window.WebSocket;
14+
const WebSocketOrig = window.WebSocket;
15+
window.WebSocket = function fakeConstructor(dt, config) {
16+
const websocket_instant = new WebSocketOrig(dt, config);
17+
websocket_instant.addEventListener("message", async function (achunk) {
18+
const utf8_str = new TextDecoder("utf-8").decode(achunk.data);
19+
// Do something here
20+
console.log(utf8_str);
21+
});
22+
return websocket_instant;
23+
};
24+
window.WebSocket.prototype = WebSocketOrig.prototype;
25+
window.WebSocket.prototype.constructor = window.WebSocket;
2626
// window.addEventListener(
2727
// "message",
2828
// function (t) {
2929
// t.source == window && console.log(t);
3030
// },
3131
// !1
3232
// );
33-
34-
let emptyFunc = void 0;
35-
Object.defineProperty(window, "__d", {
36-
get: () => emptyFunc,
37-
set: (i) => {
38-
const c = new Proxy(i, {
39-
apply: function (target, thisArg, arg) {
40-
console.log(thisArg);
41-
return target(...arg);
42-
},
43-
});
44-
emptyFunc = c;
45-
},
46-
});
33+
// let emptyFunc = void 0;
34+
// Object.defineProperty(window, "__d", {
35+
// get: () => emptyFunc,
36+
// set: (i) => {
37+
// const c = new Proxy(i, {
38+
// apply: function (target, thisArg, arg) {
39+
// console.log(thisArg);
40+
// return target(...arg);
41+
// },
42+
// });
43+
// emptyFunc = c;
44+
// },
45+
// });
4746
},
48-
4947
onDocumentIdle: () => {
5048
// tất cả loại tin nhắn đều được bao bọc bởi:
5149
// MWPBaseMessage.bs
@@ -140,19 +138,5 @@ export default {
140138
// return LSUpdateTypingIndicatorOrig.apply(this, arguments);
141139
// };
142140
});
143-
144-
let emptyFunc = void 0;
145-
Object.defineProperty(window, "__d", {
146-
get: () => emptyFunc,
147-
set: (i) => {
148-
const c = new Proxy(i, {
149-
apply: function (target, thisArg, arg) {
150-
console.log(thisArg);
151-
return target(...arg);
152-
},
153-
});
154-
emptyFunc = c;
155-
},
156-
});
157141
},
158142
};

0 commit comments

Comments
 (0)