Skip to content

Commit 30eeba3

Browse files
committed
.
1 parent 3ccdec7 commit 30eeba3

File tree

6 files changed

+35
-27
lines changed

6 files changed

+35
-27
lines changed

empty_script.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { runScript, runScriptInCurrentTab } from "./helpers/utils.js";
2-
31
export default {
42
icon: "",
53
name: {
@@ -13,10 +11,6 @@ export default {
1311
blackList: [],
1412
whiteList: [],
1513

16-
// Check if this script is on (show checkmark on UI)
17-
getActive: () => {},
18-
setActive: () => {},
19-
2014
// run (if active) in background context
2115
backgroundScript: {
2216
onDocumentStart: (tab) => {},

popup/index.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import { GlobalBlackList, MsgType } from "../scripts/helpers/constants.js";
1+
import {
2+
Events,
3+
GlobalBlackList,
4+
MsgType,
5+
ScriptType,
6+
} from "../scripts/helpers/constants.js";
27
import {
38
checkBlackWhiteList,
9+
getActiveScript,
410
getCurrentTab,
511
isFunction,
612
removeAccents,
7-
runScriptInCurrentTab,
813
sendEventToTab,
14+
toggleActiveScript,
915
} from "../scripts/helpers/utils.js";
1016
import { allScripts } from "../scripts/index.js";
1117
import { checkForUpdate } from "./helpers/checkForUpdate.js";
@@ -146,12 +152,14 @@ function createScriptButton(script, isFavorite = false) {
146152
buttonContainer.className = "buttonContainer";
147153

148154
// button checker
149-
if (isFunction(script.getActive)) {
155+
if (
156+
ScriptType.contentScript in script ||
157+
ScriptType.backgroundScript in script
158+
) {
150159
const checkmark = document.createElement("button");
151160
checkmark.className = "checkmark tooltip";
152161
checkmark.onclick = async (e) => {
153-
let newValue = !(await script.getActive());
154-
await script.setActive(newValue);
162+
let newValue = await toggleActiveScript(script.id);
155163
updateButtonChecker(script, buttonContainer, newValue);
156164
};
157165

@@ -264,7 +272,7 @@ function createScriptButton(script, isFavorite = false) {
264272
async function updateButtonChecker(script, button, val) {
265273
let checkmark = button.querySelector(".checkmark");
266274
if (!checkmark) return;
267-
if (val ?? (await script.getActive?.())) {
275+
if (val ?? (await getActiveScript(script.id))) {
268276
checkmark.classList.add("active");
269277
checkmark.title = t({
270278
vi: "Tắt tự động chạy",

popup/tabs.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ScriptType } from "../scripts/helpers/constants.js";
12
import { isFunction } from "../scripts/helpers/utils.js";
23
import { allScripts as s } from "../scripts/index.js";
34
import { CATEGORY } from "./helpers/category.js";
@@ -6,7 +7,10 @@ import { favoriteScriptsSaver, recentScriptsSaver } from "./helpers/storage.js";
67
const createTitle = (en, vi) => ({ name: { en, vi } });
78
const canClick = (script) =>
89
isFunction(script.onClick) || isFunction(script.onClickExtension);
9-
const isTitle = (script) => !canClick(script);
10+
const isTitle = (script) =>
11+
!canClick(script) &&
12+
!(ScriptType.contentScript in script) &&
13+
!(ScriptType.backgroundScript in script);
1014

1115
const specialTabs = [
1216
{

scripts/content-scripts/document_start.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
ScriptType.contentScript,
3232
location.href
3333
);
34+
3435
sendEventToBackground({
3536
type: MsgType.runScript,
3637
event: Events.document_start,

scripts/fb_toggleLight.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
import {
2-
getCurrentTab,
3-
localStorage,
4-
runScriptInCurrentTab,
5-
runScriptInTab,
6-
} from "./helpers/utils.js";
7-
8-
const key = "ufs-fb-toggle-light";
9-
101
export default {
112
icon: `<i class="fa-solid fa-lightbulb"></i>`,
123
name: {
@@ -19,9 +10,6 @@ export default {
1910
},
2011
whiteList: ["https://www.facebook.com"],
2112

22-
getActive: async () => await await localStorage.get(key),
23-
setActive: async (v) => await await localStorage.set(key, v),
24-
2513
contentScript: {
2614
onDocumentIdle: () => {
2715
shared.toggleLight(false);

scripts/helpers/utils.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export async function runAllScriptWithEventType(eventType, scriptType, url) {
1010

1111
let func = script?.[scriptType]?.[funcName];
1212
if (isFunction(func) && !isEmptyFunction(func)) {
13-
let isActive = (await script.getActive?.()) ?? true;
13+
let isActive = (await getActiveScript(script.id)) ?? true;
1414
isActive && func();
1515
console.log(
1616
`%c > Run ${script.id} ${scriptType} ${funcName}`,
@@ -37,14 +37,27 @@ export async function sendEventToTab(tabId, data) {
3737
// https://developer.chrome.com/docs/extensions/reference/storage/
3838
export const localStorage = {
3939
set: async (key, value) => {
40-
return chrome.storage.sync.set({ [key]: value });
40+
await chrome.storage.sync.set({ [key]: value });
41+
return value;
4142
},
4243
get: async (key, defaultValue = "") => {
4344
let result = await chrome.storage.sync.get([key]);
4445
return result[key] || defaultValue;
4546
},
4647
};
4748

49+
export async function setActiveScript(scriptId, isActive = true) {
50+
return await localStorage.set("isactive" + scriptId, isActive);
51+
}
52+
53+
export async function getActiveScript(scriptId) {
54+
return await localStorage.get("isactive" + scriptId, false);
55+
}
56+
57+
export async function toggleActiveScript(scriptId) {
58+
return await setActiveScript(scriptId, !(await getActiveScript(scriptId)));
59+
}
60+
4861
// #endregion
4962

5063
// #region Tab Utils

0 commit comments

Comments
 (0)