Skip to content

Commit

Permalink
fix: use the correct branch for fetching rules
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperlapse122 committed Jul 16, 2024
1 parent 4b277e7 commit a4119ae
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 33 deletions.
51 changes: 33 additions & 18 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 = */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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}`),
});
Expand Down
19 changes: 11 additions & 8 deletions src/common/adblocks/namuwiki.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
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).
*
* That's totally giving content creators a fuck. But many people are not using
* 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
Expand All @@ -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<typeof browser.declarativeNetRequest.updateDynamicRules>[0]['addRules'] {
Expand All @@ -58,13 +56,18 @@ export function getAdBlockers(): Parameters<typeof browser.declarativeNetRequest
}

export async function unregisterDynamicRules(rules: Parameters<typeof browser.declarativeNetRequest.updateDynamicRules>[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<typeof unregisterDynamicRules>[0]) {
await unregisterDynamicRules(rules);
console.log('Rules for register:', rules);
await browser.declarativeNetRequest.updateDynamicRules({
addRules: rules,
});
Expand Down
6 changes: 3 additions & 3 deletions src/common/global.ts
Original file line number Diff line number Diff line change
@@ -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<typeof fetch>;

Expand Down Expand Up @@ -39,5 +39,5 @@ export function fetchRepo(input: FetchParams[0], init?: FetchParams[1] & { repo:
}
}

return fetch(newInput, init);
return fetch(newInput, init as RequestInit);
}
8 changes: 4 additions & 4 deletions src/common/rules/block.ts
Original file line number Diff line number Diff line change
@@ -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[];

Expand Down Expand Up @@ -46,16 +46,16 @@ async function fetchOnlineBlockRules(): Promise<BlockedSite[]> {
*/
export async function getOnlineBlockRules(): Promise<BlockedSite[]> {
let config = await getConfig();

if (config?.blocked?.onlineRules === undefined) {
try {
console.log('fetching online block rules');
await fetchOnlineBlockRules();
config = getConfig();
} catch(e) {
console.error("Failed to fetch online block rules", e);
return [];
}

return [];
}

return config.blocked.onlineRules.map(BlockedSite.fromSerialized);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
],
"target": "ES2017",
"module": "ES6",
"moduleResolution": "Node",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
Expand Down

0 comments on commit a4119ae

Please sign in to comment.