diff --git a/src/background.ts b/src/background.ts index fbedb48..b257258 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1,28 +1,42 @@ import browser from 'webextension-polyfill'; -import { getConfig, loadConfig } from './common/config/index'; -import { loadAll } from './common/initializer'; -import { getAdBlockers, reregisterDynamicRules, unregisterDynamicRules } from './common/adblocks/namuwiki'; -import { checkIfIntelliBanPass } from './common/intelliBan/index'; -import { isArcaLiveBlocked, isNamuNewsBlocked } from './common/utils'; -import { getRedirectTargets, handleRedirects, loadRedirectionRules } from './common/rules/redirect'; -import { getActiveRulesFromConfig } from './common/rules/enabled'; -import { runSearchFilterRoutine } from './searchFilters/runner'; -import { ConfigInterface } from './common/config/interface'; -import { loadBlockRules } from './common/rules/block'; +import {getConfig, loadConfig} from './common/config'; +import {loadAll} from './common/initializer'; +import {getAdBlockers, reregisterDynamicRules, unregisterDynamicRules} from './common/adblocks/namuwiki'; +import {checkIfIntelliBanPass} from './common/intelliBan'; +import {isNamuNewsBlocked} from './common/utils'; +import {handleRedirects} from './common/rules/redirect'; +import {getActiveRulesFromConfig} from './common/rules/enabled'; +import {runSearchFilterRoutine} from './searchFilters/runner'; +import {ConfigInterface} from './common/config/interface'; +import {loadBlockRules} from './common/rules/block'; /* = Tab Context Save = */ const previousTabUrls: string[] = []; const syncData = async () => { + let config: ConfigInterface | undefined = undefined; console.log('Syncing Data'); - const config = await loadConfig(); - await loadAll(); + + try { + config = await loadConfig(); + await loadAll(); + } catch (e) { + console.error('Failed to load config', e); + return; + } + console.log('Synced config', config); - await updateDynamicRules(config); + + try { + await updateDynamicRules(config!); + } catch (e) { + console.error('Failed to sync dynamic rules', e); + } } const updateDynamicRules = async (config: ConfigInterface) => { - console.log(await browser.declarativeNetRequest.getDynamicRules()); + const dynamicRules=await browser.declarativeNetRequest.getDynamicRules(); + console.log(dynamicRules); const adBlockers = getAdBlockers(); if (config?.adblock?.namuwiki) { @@ -35,9 +49,10 @@ const updateDynamicRules = async (config: ConfigInterface) => { /* = Initial Load = */ (async () => { - const config = await loadConfig(); + // const config = await loadConfig(); + await loadConfig(); await loadAll(); - await updateDynamicRules(config); + // await updateDynamicRules(config); })(); /* = On Install = */ @@ -86,7 +101,7 @@ browser.tabs.onUpdated.addListener(async (tabId, info, tab) => { target: {tabId: tab.id}, func: runSearchFilterRoutine, args: [rules], - }) + }) } } catch (e) { console.error('Script chainloading failed: ', e); @@ -114,7 +129,7 @@ browser.tabs.onUpdated.addListener(async (tabId, info, tab) => { if (checkIfIntelliBanPass(query)) { return; } - + await browser.tabs.update(tabId, { url: browser.runtime.getURL(`ui/banned/index.html?banned_url=${url}&site_name=${matchingRule.name}`), }); diff --git a/src/common/adblocks/namuwiki.ts b/src/common/adblocks/namuwiki.ts index 378caff..486865e 100644 --- a/src/common/adblocks/namuwiki.ts +++ b/src/common/adblocks/namuwiki.ts @@ -1,12 +1,12 @@ import browser from "webextension-polyfill"; -import { getActiveRules } from "../rules/enabled"; +import {getActiveRules} from "../rules/enabled"; /** * Why AdBlock NamuWiki? * * Contents of namuwiki is distributed under Creative Commons-BY-NC-SA License. * which DOESN'T allow webpage to create their ad-revenue or sell the content - * with their content, BUT, Current owner of namuwiki is literally *selling* + * with their content, BUT a Current owner of namuwiki is literally *selling* * content by violating namuwiki's license before acquisition (even they are * still using CC-BY-NC-SA License). * @@ -14,7 +14,7 @@ import { getActiveRules } from "../rules/enabled"; * ad-block to support the creators, and actually, Namuwiki is still in the * Acceptable-Ads lists. * - * which is un-acceptable for me entirely because they are earning their + * which is unacceptable for me entirely because they are earning their * ad-revenue by copyright infringement. * * From Version 0.6.0, I am boycotting namuwiki's ad-revenue system by @@ -24,14 +24,12 @@ import { getActiveRules } from "../rules/enabled"; */ function getNamuwikiInitDomains() { - const namuwikiInitDomains = getActiveRules([ + return getActiveRules([ 'namuwiki', 'namuwikiMirror', 'namulive', 'namunews', - ])?.map(n => 'https://'+n.baseURL).map(n => new URL(n).hostname); - - return namuwikiInitDomains; + ])?.map(n => 'https://' + n.baseURL).map(n => new URL(n).hostname); } export function getAdBlockers(): Parameters[0]['addRules'] { @@ -58,13 +56,18 @@ export function getAdBlockers(): Parameters[0]['addRules']) { + const ruleIdsForRemove=[...(rules ?? []).map(n => n.id)]; + + console.log('Rule Ids for remove:', ruleIdsForRemove); + await browser.declarativeNetRequest.updateDynamicRules({ - removeRuleIds: [...(rules ?? []).map(n => n.id)], + removeRuleIds: ruleIdsForRemove, }); } export async function reregisterDynamicRules(rules: Parameters[0]) { await unregisterDynamicRules(rules); + console.log('Rules for register:', rules); await browser.declarativeNetRequest.updateDynamicRules({ addRules: rules, }); diff --git a/src/common/global.ts b/src/common/global.ts index 506d1a0..c269fe7 100644 --- a/src/common/global.ts +++ b/src/common/global.ts @@ -1,5 +1,5 @@ -const targetBranch = 'main'; -const basePath = 'https://raw.githubusercontent.com/Alex4386/turnoff-namuwiki/'+(targetBranch ?? 'main')+'/'; +const targetBranch = 'master'; +const basePath = `https://raw.githubusercontent.com/Alex4386/turnoff-namuwiki/${targetBranch}/`; type FetchParams = Parameters; @@ -39,5 +39,5 @@ export function fetchRepo(input: FetchParams[0], init?: FetchParams[1] & { repo: } } - return fetch(newInput, init); + return fetch(newInput, init as RequestInit); } diff --git a/src/common/rules/block.ts b/src/common/rules/block.ts index e084ec1..d436573 100644 --- a/src/common/rules/block.ts +++ b/src/common/rules/block.ts @@ -1,9 +1,9 @@ import browser from 'webextension-polyfill'; -import { getConfig, loadConfig } from '../config/index'; +import { getConfig, loadConfig } from '../config'; import { fetchRepo } from '../global'; import { BlockedSite } from './model'; import { SerializedBlockedSite } from './interface'; -import { serializeRegex } from '../regex/index'; +import { serializeRegex } from '../regex'; let blockRulesCache: BlockedSite[]; @@ -46,6 +46,7 @@ async function fetchOnlineBlockRules(): Promise { */ export async function getOnlineBlockRules(): Promise { let config = await getConfig(); + if (config?.blocked?.onlineRules === undefined) { try { console.log('fetching online block rules'); @@ -53,9 +54,8 @@ export async function getOnlineBlockRules(): Promise { config = getConfig(); } catch(e) { console.error("Failed to fetch online block rules", e); + return []; } - - return []; } return config.blocked.onlineRules.map(BlockedSite.fromSerialized); diff --git a/tsconfig.json b/tsconfig.json index 0401260..a91ea11 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,7 @@ ], "target": "ES2017", "module": "ES6", + "moduleResolution": "Node", "outDir": "./dist", "rootDir": "./src", "strict": true,