Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
improvement: removing ads based on location
Browse files Browse the repository at this point in the history
  • Loading branch information
MidKnightXI committed Dec 7, 2022
1 parent e8ec583 commit 242bac7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 39 deletions.
4 changes: 0 additions & 4 deletions .prettierrc

This file was deleted.

84 changes: 57 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { extractAll, createPackageWithOptions } from "asar";
import { readdirSync, readFileSync, writeFileSync, existsSync } from "fs";
import { sync } from "rimraf";
import { extractAll, createPackageWithOptions } from "@electron/asar";
import { readdirSync, readFileSync, writeFile, existsSync } from "fs";
import rimraf from "rimraf";
import { dirname, normalize } from "path";
import { spawn } from "child_process";
import { exit } from "process";

function replaceAdFileContent(path) {
function patchFile(path) {
let content = readFileSync(path).toString();

const adsense_patch = "https://gist.githubusercontent.com/MidKnightXI/7ecf3cdd0a5804466cb790855e2524ae/raw/9b88cf64f3bb955edfff27bdfba72f5181d8748b/remover.txt";
const AMERICA = '["US","CA"].includes';
const EU = '["AD","AL","AT","AX","BA","BE","BG","BY","CH","CY","CZ","DE","DK","EE","ES","FI","FO","FR","GB","GG","GI","GR","HR","HU","IE","IM","IS","IT","JE","LI","LT","LU","LV","MC","MD","ME","MK","MT","NL","NO","PL","PT","RO","RS","RU","SE","SI","SJ","SK","SM","UA","VA","XK"].includes';

content = content.replaceAll(
"https://dtapp-player.op.gg/adsense.txt",
"https://gist.githubusercontent.com/MidKnightXI/7ecf3cdd0a5804466cb790855e2524ae/raw/9b88cf64f3bb955edfff27bdfba72f5181d8748b/remover.txt"
adsense_patch
);
content = content.replace(
/exports\.countryHasAds=\w;/gm,
Expand All @@ -31,10 +36,20 @@ function replaceAdFileContent(path) {
/exports\.nitropayAds=\w;/gm,
"exports.nitropayAds=[];"
);
writeFileSync(path, content);

content = content.replaceAll(
"google-analytics.com/mp/collect",
"gist.githubusercontent.com"
);

content = content.replaceAll(AMERICA, "[].includes");
content = content.replaceAll(EU, "[].includes");

writeFile(path, content, () => console.log(`PatchFile: rewriting ${path}`));
return;
}

async function rebuildAddDir(asarFilePath) {
async function scanDir(asarFilePath) {
console.log("Unpacking OPGG asar file");
extractAll(asarFilePath, "op-gg-unpacked");

Expand All @@ -43,8 +58,7 @@ async function rebuildAddDir(asarFilePath) {

for (let fileName of assetFiles) {
if (fileName.endsWith(".js")) {
console.log(`Patching: ${fileName}`);
replaceAdFileContent(normalize(`${assetDir}/${fileName}`));
patchFile(normalize(`${assetDir}/${fileName}`));
}
}

Expand All @@ -53,34 +67,50 @@ async function rebuildAddDir(asarFilePath) {
unpackDir: "{node_modules/node-ovhook,node_modules/rust-process}",
});

console.log(`Deleted temporary directory`);
sync("op-gg-unpacked");
rimraf("op-gg-unpacked", () => {
console.log(`scanDir: deleted temporary directory`)
});
return;
}

function killOpgg() {
console.log("Killing OPGG process");
process.platform === "darwin"
? spawn("killall", ["-9", "OP.GG"])
: spawn("taskkill", ["/im", "OP.GG.exe", "/F"]);
function killProcess() {
console.log("killProcess: killing OPGG process");

if (process.platform === "darwin") {
spawn("killall", ["-9", "OP.GG"]);
}
else if (process.platform === "win32") {
spawn("taskkill", ["/im", "OP.GG.exe", "/F"]);
}
return;
}

function asarDir() {
let directory = null;

if (process.platform === "darwin") {
directory = normalize("/Applications/OP.GG.app/Contents/Resources/app.asar");
} else if (process.platform === "win32") {
directory = normalize(
`${dirname(process.env.APPDATA)}/Local/Programs/OP.GG/resources/app.asar`);
}
else {
console.error("asarDir: platform not supported.")
exit(0);
}
return directory;
}

function main() {
const asarFilePath =
process.platform === "darwin"
? normalize("/Applications/OP.GG.app/Contents/Resources/app.asar")
: normalize(
`${dirname(
process.env.APPDATA
)}/Local/Programs/OP.GG/resources/app.asar`
);
const asarFilePath = asarDir();

if (!existsSync(asarFilePath)) {
console.log(`Cannot find asar file at ${asarFilePath}`);
console.error(`Cannot find asar file at ${asarFilePath}`);
return 84;
}
killOpgg();
rebuildAddDir(asarFilePath);
killProcess();
scanDir(asarFilePath);
return;
}

main();
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@
"license": "MIT",
"type": "module",
"scripts": {
"format:check": "prettier --check .",
"format:write": "prettier --write .",
"lint:check": "eslint .",
"lint:fix": "eslint --fix .",
"bundle": "rollup --config",
"build": "rollup -c && pkg -o opgg-ads-remover -t node16-win-x64,node16-darwin-x64 bundle.js && chmod 755 opgg-ads-remover-macos",
"build:win": "rollup -c && pkg -o opgg-ads-remover-win -t node16-win-x64 bundle.js",
"build:win-arm": "rollup -c && pkg -o opgg-ads-remover-win-arm -t node16-win-arm64 bundle.js",
"build:darwin": "rollup -c && pkg -o opgg-ads-remover-darwin -t node16-darwin-x64 bundle.js && chmod 755 opgg-ads-remover-darwin"
"build": "rollup -c && pkg -o opgg-ads-remover -t node16-win-x64,node16-darwin-x64 bundle.js && chmod 755 opgg-ads-remover-macos && rm -f bundle.js",
"build:win": "rollup -c && pkg -o opgg-ads-remover-win -t node16-win-x64 bundle.js && rm -f bundle.js",
"build:win-arm": "rollup -c && pkg -o opgg-ads-remover-win-arm -t node16-win-arm64 bundle.js && rm -f bundle.js",
"build:darwin": "rollup -c && pkg -o opgg-ads-remover-darwin -t node16-darwin-x64 bundle.js && chmod 755 opgg-ads-remover-darwin && rm -f bundle.js"
},
"dependencies": {
"asar": "3.1.0",
"@electron/asar": "^3.2.2",
"child_process": "^1.0.2",
"fs": "^0.0.1-security",
"path": "^0.12.7",
Expand All @@ -29,7 +27,6 @@
"eslint": "^8.17.0",
"eslint-config-prettier": "^8.5.0",
"pkg": "^5.7.0",
"prettier": "^2.6.2",
"rollup": "^2.75.6"
}
}

0 comments on commit 242bac7

Please sign in to comment.