|
2 | 2 |
|
3 | 3 | clearJavascriptRule, |
4 | 4 | getTabSetting, |
| 5 | + removeConflictedRulesFromPattern, |
5 | 6 | removeJavascriptRule, |
6 | 7 | setJavascriptRule, |
7 | 8 | } from "./contentsettings"; |
@@ -53,35 +54,46 @@ export const toggleJavaScript = async (tab: chrome.tabs.Tab) => { |
53 | 54 | const { subdomain, scheme } = await getUrlAsObject(tab.url!); |
54 | 55 | const setting = await getTabSetting(tab); |
55 | 56 | 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); |
66 | 60 | } else { |
67 | | - if (scheme === "file") { |
68 | | - await handleAllowUrl(tab); |
| 61 | + if (subdomain.length) { |
| 62 | + await handleToggleSubdomain(tab); |
69 | 63 | } 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); |
83 | 65 | } |
84 | 66 | } |
| 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 | + // } |
85 | 97 | } |
86 | 98 |
|
87 | 99 | // if (blockedSubdomainAndDomain) { |
@@ -262,6 +274,55 @@ export const handlePause = async (tab: chrome.tabs.Tab) => { |
262 | 274 | }); |
263 | 275 | }; |
264 | 276 |
|
| 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 | + |
265 | 326 | export const handleBlockSubdomain = async (tab: chrome.tabs.Tab) => { |
266 | 327 | if (tab.url) { |
267 | 328 | const primaryPattern = getSubdomainPatternFromUrl(tab.url); |
|
0 commit comments