Skip to content

Commit 51bc3da

Browse files
author
hoang.tran12
committed
wip
1 parent 8883a71 commit 51bc3da

File tree

8 files changed

+118
-52
lines changed

8 files changed

+118
-52
lines changed

popup/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { GlobalBlackList, MsgType } from "../scripts/helpers/constants.js";
22
import {
33
checkBlackWhiteList,
4-
getActiveScript,
4+
isActiveScript,
55
getCurrentTab,
66
isFunction,
77
removeAccents,
@@ -174,8 +174,8 @@ function createScriptButton(script, isFavorite = false) {
174174
button.onclick = () =>
175175
alert(
176176
t({
177-
vi: "Chức năng này tự động chạy\nTắt/Mở tự chạy bằng nút bên trái",
178-
en: "This function is Autorun\nTurn on/off autorun by click the left checkmark",
177+
vi: "Chức năng này tự động chạy.\nTắt/Mở tự chạy bằng nút bên trái.\nSau đó tải lại trang web.",
178+
en: "This function is Autorun.\nTurn on/off autorun by click the left checkmark.\nThen reload the webpage.",
179179
})
180180
);
181181
} else {
@@ -278,7 +278,7 @@ function createScriptButton(script, isFavorite = false) {
278278
async function updateButtonChecker(script, button, val) {
279279
let checkmark = button.querySelector(".checkmark");
280280
if (!checkmark) return;
281-
if (val ?? (await getActiveScript(script.id))) {
281+
if (val ?? (await isActiveScript(script.id))) {
282282
checkmark.classList.add("active");
283283
checkmark.title = t({
284284
vi: "Tắt tự động chạy",

popup/tabs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ const tabs = [
229229
s.reEnableContextMenu,
230230
s.showHiddenFields,
231231
s.studyphim_unlimited,
232-
s.envato_bypassPreview,
233232
s.studocu_bypassPreview,
233+
s.envato_bypassPreview,
234234
s.viewCookies,
235235
s.removeCookies,
236236
s.viewBrowserInfo,

scripts/background-scripts/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Events } from "../helpers/constants.js";
33
import { MsgType } from "../helpers/constants.js";
44
import {
55
checkBlackWhiteList,
6-
getActiveScript,
6+
isActiveScript,
77
isEmptyFunction,
88
isFunction,
99
runScriptInTab,
@@ -25,7 +25,6 @@ chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
2525

2626
async function runScript(event, tab) {
2727
let funcName = event,
28-
count = 0,
2928
tabId = tab.id,
3029
url = tab.url;
3130

@@ -38,7 +37,7 @@ async function runScript(event, tab) {
3837

3938
let func = script[funcName];
4039
if (isFunction(func) && !isEmptyFunction(func)) {
41-
let isActive = (await getActiveScript(script.id)) ?? true;
40+
let isActive = (await isActiveScript(script.id)) ?? true;
4241
if (isActive) {
4342
runScriptInTab({ func, tabId });
4443
console.log(
Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,64 @@
1+
// https://stackoverflow.com/a/70949953
2+
// https://stackoverflow.com/a/9517879
3+
14
(async () => {
2-
try {
3-
const { MsgType, Events, ClickType } = await import(
4-
"../helpers/constants.js"
5-
);
6-
const { sendEventToBackground, isFunction } = await import(
7-
"../helpers/utils.js"
8-
);
9-
10-
sendEventToBackground({
11-
type: MsgType.runScript,
12-
event: Events.onDocumentStart,
13-
});
14-
15-
chrome.runtime.onMessage.addListener(async function (
16-
message,
17-
sender,
18-
sendResponse
19-
) {
20-
console.log("> Received message:", message);
21-
22-
switch (message.type) {
23-
case MsgType.runScript:
24-
let scriptId = message.scriptId;
25-
const script = (await import("../" + scriptId + ".js"))?.default;
26-
27-
if (script && isFunction(script[ClickType.onClickContentScript])) {
28-
script[ClickType.onClickContentScript]();
29-
console.log("> Run script " + scriptId);
30-
}
31-
break;
32-
}
33-
});
34-
35-
// https://stackoverflow.com/a/53033388
36-
const { getURL, injectScript, injectCss } = await import("./utils.js");
37-
injectScript(getURL("useful-scripts-utils.js"));
38-
} catch (e) {
39-
console.log("ERROR: ", e);
5+
function injectScript(url) {
6+
var s = document.createElement("script");
7+
s.src = url;
8+
(document.head || document.documentElement).appendChild(s);
9+
console.log("Useful-scripts injected " + url);
10+
s.remove();
4011
}
12+
13+
let key = "activeScripts";
14+
let ids = (await chrome.storage.sync.get([key]))?.[key];
15+
16+
injectScript(
17+
chrome.runtime.getURL("/scripts/content-scripts/load_scripts.js") +
18+
"?ids=" +
19+
ids
20+
);
4121
})();
22+
23+
// (async () => {
24+
// try {
25+
// const { MsgType, Events, ClickType } = await import(
26+
// "../helpers/constants.js"
27+
// );
28+
// const { sendEventToBackground, isFunction } = await import(
29+
// "../helpers/utils.js"
30+
// );
31+
32+
// sendEventToBackground({
33+
// type: MsgType.runScript,
34+
// event: Events.onDocumentStart,
35+
// });
36+
37+
// chrome.runtime.onMessage.addListener(async function (
38+
// message,
39+
// sender,
40+
// sendResponse
41+
// ) {
42+
// console.log("> Received message:", message);
43+
44+
// switch (message.type) {
45+
// case MsgType.runScript:
46+
// let scriptId = message.scriptId;
47+
// const script = (await import("../" + scriptId + ".js"))?.default;
48+
49+
// if (script && isFunction(script[ClickType.onClickContentScript])) {
50+
// script[ClickType.onClickContentScript]();
51+
// console.log("> Run script " + scriptId);
52+
// }
53+
// break;
54+
// }
55+
// });
56+
57+
// // https://stackoverflow.com/a/53033388
58+
// const { getURL, injectScript, injectCss } = await import("./utils.js");
59+
// injectScript(getURL("content-scripts/scripts/useful-scripts-utils.js"));
60+
// // injectScript(getURL("content-scripts/load_script.js"), "module");
61+
// } catch (e) {
62+
// console.log("ERROR: ", e);
63+
// }
64+
// })();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const params = new URLSearchParams(document.currentScript.src.split("?")?.[1]);
2+
console.log(params);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// prevent auto redirect from https://mp3.zing.vn/ to https://zingmp3.vn/
2+
window.MP3_MEDIA_USER_UPLOAD = 1;
3+
4+
// mp3 VIP (test)
5+
window.onload = () => {
6+
if (window.MP3) {
7+
window.MP3.ACCOUNT_ID = new Date().getTime();
8+
window.MP3.ACCOUNT_NAME = "VIP - Useful scripts";
9+
window.MP3.VIP = 1;
10+
window.MP3.IS_IP_VN = true;
11+
}
12+
13+
window.checkLogin = () => true;
14+
15+
if (window.ZVip) {
16+
window.ZVip.isVip = 1;
17+
window.ZVip.vip = 1;
18+
}
19+
};

scripts/content-scripts/utils.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
export function getURL(fileName) {
2-
return "/scripts/content-scripts/scripts/" + fileName;
2+
return "/scripts/" + fileName;
33
}
44

5-
export function injectScript(filePathOrUrl, isExternal = false) {
5+
export function injectScript(
6+
filePathOrUrl,
7+
type = "application/javascript",
8+
isExternal = false
9+
) {
610
try {
711
var s = document.createElement("script");
812
s.src = isExternal ? filePathOrUrl : chrome.runtime.getURL(filePathOrUrl);
13+
s.type = type;
914
s.onload = function () {
1015
console.log("Useful-scripts injected " + s.src);
1116
this.remove();

scripts/helpers/utils.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,32 @@ export const localStorage = {
2828
},
2929
};
3030

31+
const listActiveScriptsKey = "activeScripts";
3132
export async function setActiveScript(scriptId, isActive = true) {
32-
return await localStorage.set("isactive" + scriptId, isActive);
33+
let list = await getAllActiveScriptId();
34+
if (isActive) {
35+
list.push(scriptId);
36+
} else {
37+
list = list.filter((_) => _ != scriptId);
38+
}
39+
await localStorage.set(listActiveScriptsKey, list.join(","));
40+
return list;
41+
}
42+
43+
export async function isActiveScript(scriptId) {
44+
let currentList = await getAllActiveScriptId();
45+
return currentList.find((_) => _ == scriptId) != null;
3346
}
3447

35-
export async function getActiveScript(scriptId) {
36-
return await localStorage.get("isactive" + scriptId, false);
48+
export async function getAllActiveScriptId() {
49+
return (await localStorage.get(listActiveScriptsKey, "")).split(",");
3750
}
3851

3952
export async function toggleActiveScript(scriptId) {
40-
return await setActiveScript(scriptId, !(await getActiveScript(scriptId)));
53+
let current = await isActiveScript(scriptId);
54+
let newVal = !current;
55+
await setActiveScript(scriptId, newVal);
56+
return newVal;
4157
}
4258

4359
// #endregion
@@ -98,6 +114,7 @@ export const runScriptInTab = async ({ func, tabId, args = [] }) => {
98114
func: func,
99115
args: args,
100116
world: chrome.scripting.ExecutionWorld.MAIN,
117+
injectImmediately: true,
101118
},
102119
(injectionResults) => {
103120
// https://developer.chrome.com/docs/extensions/reference/scripting/#handling-results
@@ -115,6 +132,7 @@ export const runScriptFile = ({ scriptFile, tabId, args = [] }) => {
115132
files: [scriptFile],
116133
args: args,
117134
world: chrome.scripting.ExecutionWorld.MAIN,
135+
injectImmediately: true,
118136
},
119137
(injectionResults) => {
120138
// https://developer.chrome.com/docs/extensions/reference/scripting/#handling-results

0 commit comments

Comments
 (0)