Skip to content

Commit 06bc105

Browse files
return to the classic allow / block behaviour (instead of clear) to prevent bugs, need to investigate, but later
1 parent d548774 commit 06bc105

File tree

4 files changed

+105
-31
lines changed

4 files changed

+105
-31
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "qjs",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"private": true,
55
"scripts": {
66
"serve": "vue-cli-service serve",

src/entry/background/actions.ts

Lines changed: 86 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22

33
clearJavascriptRule,
44
getTabSetting,
5+
removeConflictedRulesFromPattern,
56
removeJavascriptRule,
67
setJavascriptRule,
78
} from "./contentsettings";
@@ -53,35 +54,46 @@ export const toggleJavaScript = async (tab: chrome.tabs.Tab) => {
5354
const { subdomain, scheme } = await getUrlAsObject(tab.url!);
5455
const setting = await getTabSetting(tab);
5556
cl(`setting for ${tab.url} : ${setting}`, Log.ACTIONS);
56-
if (setting === "allow") {
57-
if (scheme === "file") {
58-
await handleBlockUrl(tab);
59-
} else {
60-
if (subdomain.length) {
61-
await handleBlockSubdomain(tab);
62-
} else {
63-
await handleBlockDomain(tab);
64-
}
65-
}
57+
58+
if (scheme === "file") {
59+
await handleToggleUrl(tab);
6660
} else {
67-
if (scheme === "file") {
68-
await handleAllowUrl(tab);
61+
if (subdomain.length) {
62+
await handleToggleSubdomain(tab);
6963
} else {
70-
if (subdomain.length) {
71-
if (tab.incognito === true) {
72-
await handleAllowSubdomain(tab);
73-
} else {
74-
await handleClearSubdomain(tab);
75-
}
76-
} else {
77-
if (tab.incognito === true) {
78-
await handleAllowDomain(tab);
79-
} else {
80-
await handleClearDomain(tab);
81-
}
82-
}
64+
await handleToggleDomain(tab);
8365
}
8466
}
67+
68+
// if (setting === "allow") {
69+
// if (scheme === "file") {
70+
// await handleBlockUrl(tab);
71+
// } else {
72+
// if (subdomain.length) {
73+
// await handleBlockSubdomain(tab);
74+
// } else {
75+
// await handleBlockDomain(tab);
76+
// }
77+
// }
78+
// } else {
79+
// if (scheme === "file") {
80+
// await handleAllowUrl(tab);
81+
// } else {
82+
// if (subdomain.length) {
83+
// if (tab.incognito === true) {
84+
// await handleAllowSubdomain(tab);
85+
// } else {
86+
// await handleClearSubdomain(tab);
87+
// }
88+
// } else {
89+
// if (tab.incognito === true) {
90+
// await handleAllowDomain(tab);
91+
// } else {
92+
// await handleClearDomain(tab);
93+
// }
94+
// }
95+
// }
96+
// }
8597
}
8698

8799
// if (blockedSubdomainAndDomain) {
@@ -262,6 +274,55 @@ export const handlePause = async (tab: chrome.tabs.Tab) => {
262274
});
263275
};
264276

277+
export const hanleToggleByPattern = async (tab: chrome.tabs.Tab, primaryPattern: string) => {
278+
279+
if (tab.url) {
280+
const oldSetting = await getTabSetting(tab)
281+
const newSetting = oldSetting === 'allow' ? 'block' : 'allow'
282+
283+
if (!primaryPattern) {
284+
return
285+
}
286+
await removeConflictedRulesFromPattern(primaryPattern)
287+
288+
console.info(`toggle from ${oldSetting} to ${newSetting}: ` + primaryPattern);
289+
await setJavascriptRule({
290+
primaryPattern,
291+
scope: getScopeSetting(tab.incognito),
292+
setting: newSetting,
293+
tab,
294+
});
295+
296+
297+
}
298+
299+
}
300+
301+
export const handleToggleSubdomain = async (tab: chrome.tabs.Tab) => {
302+
if (tab.url) {
303+
const primaryPattern = getSubdomainPatternFromUrl(tab.url);
304+
if (primaryPattern) {
305+
await hanleToggleByPattern(tab, primaryPattern)
306+
}
307+
}
308+
}
309+
310+
export const handleToggleDomain = async (tab: chrome.tabs.Tab) => {
311+
if (tab.url) {
312+
const primaryPattern = getDomainPatternFromUrl(tab.url);
313+
if (primaryPattern) {
314+
await hanleToggleByPattern(tab, primaryPattern)
315+
}
316+
}
317+
};
318+
319+
export const handleToggleUrl = async (tab: chrome.tabs.Tab) => {
320+
if (tab.url) {
321+
const primaryPattern = getUrlPatternFromUrl(tab.url);
322+
await hanleToggleByPattern(tab, primaryPattern)
323+
}
324+
};
325+
265326
export const handleBlockSubdomain = async (tab: chrome.tabs.Tab) => {
266327
if (tab.url) {
267328
const primaryPattern = getSubdomainPatternFromUrl(tab.url);

src/entry/background/contentsettings.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isValidPrimaryPattern } from "../options/computed";
22
import { clearJavascriptRules, handleClear, reloadTab } from "./actions";
3-
import { cl, isAllowedPattern, Log } from "./utils";
3+
import { cl, isAllowedPattern, Log, sortUrlsByPatternPrecedence } from "./utils";
44
import { updateIcon } from "./icon";
55
import {
66
getDomainStorageRulesFromUrl,
@@ -10,6 +10,7 @@ import {
1010
unsetStorageRule,
1111
} from "./storage";
1212
import { getUrlAsObject, isValidScheme } from "./utils";
13+
import { set } from "lodash";
1314

1415
export type RuleSetting = "allow" | "block";
1516

@@ -42,10 +43,14 @@ export const getJavascriptRuleSetting = async ({
4243
});
4344
};
4445

46+
47+
4548
export const removeConflictedRulesFromPattern = async (tabPattern: string) => {
4649
const { scheme: tabScheme, subdomain: tabSubdomain, domain: tabDomain } = getUrlAsObject(tabPattern);
4750

4851
const existingDomainRules = await getDomainStorageRulesFromUrl(tabPattern)
52+
//const existingDomainPatterns = Object.values(existingDomainRules).map(({primaryPattern}) => primaryPattern)
53+
// const sortedPatternsByPrecedence = sortUrlsByPatternPrecedence(existingDomainPatterns)
4954

5055
Object.entries(existingDomainRules).forEach(async ([storagePattern, rule]) => {
5156
const { scheme: storageScheme, subdomain: storageSubdomain, domain: storageDomain } = getUrlAsObject(storagePattern);
@@ -55,7 +60,8 @@ export const removeConflictedRulesFromPattern = async (tabPattern: string) => {
5560
cl(`Conflicted rule removed: ${storagePattern} (conflict with url: ${tabPattern})`, Log.RULES)
5661
}
5762
if ((tabSubdomain === '*.' && storageSubdomain === '') && tabDomain === storageDomain) {
58-
console.warn(`Potential conflicted rule: ${storagePattern} (conflict with url: ${tabPattern})`)
63+
await removeJavascriptRule(rule)
64+
console.warn(`Conflicted rule removed: ${storagePattern} (conflict with url: ${tabPattern})`)
5965
}
6066
})
6167

@@ -93,7 +99,7 @@ export const setJavascriptRule = ({
9399
// } else {
94100
// await addJavascriptRule(rule);
95101
// }
96-
await removeConflictedRulesFromPattern(rule.primaryPattern)
102+
//await removeConflictedRulesFromPattern(rule.primaryPattern)
97103

98104
await addJavascriptRule(rule);
99105
if (tab) {
@@ -124,8 +130,6 @@ export const clearJavascriptRule = ({
124130
return;
125131
}
126132

127-
// await removeConflictedRulesFromPattern(rule.primaryPattern)
128-
129133
await removeJavascriptRule(rule);
130134
if (tab) {
131135
await updateIcon(tab); //not needed because we update tab

src/entry/background/events.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ export const initEvents = () => {
4949
cl("ON WINDOW FOCUS CHANGED", Log.EVENTS);
5050
await updateContextMenus();
5151

52+
53+
chrome.tabs.query({active: true, windowId: windowId}, async (tabs) => {
54+
if (tabs.length > 0) {
55+
let tab = tabs[0];
56+
await updateIcon(tab);
57+
}
58+
});
59+
60+
5261
// if (state && state.popup) {
5362
// CLOSE POPUP
5463
// chrome.windows.get(state.popup.id, (window) => {

0 commit comments

Comments
 (0)