diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c187b36 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +yarn.lock + +node_modules/ +dist/ +*.plugin \ No newline at end of file diff --git a/build.js b/build.js new file mode 100644 index 0000000..38ad4e5 --- /dev/null +++ b/build.js @@ -0,0 +1,39 @@ +const esbuild = require('esbuild'); +const fs = require('fs'); +const path = require('path'); +const compress = require('compressing'); + +// 从 manifest.json 读取版本号 +const manifest = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'manifest.json'), 'utf-8')); +const version = manifest.version || '0.0.0'; + +// 输出文件名 +const pluginName = `remove-tracking-attribute-${version}.plugin`; + +(async () => { + const distDir = path.resolve(__dirname, 'dist'); + if (!fs.existsSync(distDir)) { + fs.mkdirSync(distDir); + } + + ['manifest.json', 'preview.png'].forEach(file => { + fs.copyFileSync( + path.resolve(__dirname, file), + path.resolve(distDir, file) + ); + }); + + await esbuild.build({ + entryPoints: ['./main.js'], + bundle: true, + minify: true, + target: 'chrome91', + outfile: './dist/main.js', + }); + + await compress.zip.compressDir(distDir, path.resolve(__dirname, pluginName), { + ignoreBase: true + }); + + console.log(`打包完成: ${pluginName}`); +})(); diff --git a/main.js b/main.js index 8b26eeb..0a758de 100644 --- a/main.js +++ b/main.js @@ -1,14 +1,29 @@ -plugin.onLoad(() => { - const _channalCall = channel.call; - channel.call = (name, ...args) => { - if (name === 'winhelper.setClipBoardData') { - const replacedString = args[1][0]; - if (replacedString.includes('&')) { - args[1][0] = replacedString.replace(/&userid=\d+/, ''); - } else { - args[1][0] = replacedString.replace(/userid=\d+/, ''); - } - } - _channalCall(name, ...args); - }; -}); +plugin.onLoad(() => { + const blacklist = ["userid", "uct2"]; + + const _channelCall = channel.call; + channel.call = (name, ...args) => { + if (name === "winhelper.setClipBoardData") { + let url = args[1][0]; + if (typeof url === "string") { + try { + const u = new URL(url); + const params = u.searchParams; + + blacklist.forEach(key => params.delete(key)); + + u.search = params.toString(); + args[1][0] = u.toString(); + } catch (e) { + blacklist.forEach(key => { + const regex = new RegExp(`([?&])${key}=[^&]+(&|$)`, "g"); + url = url.replace(regex, "$1"); + }); + url = url.replace(/[?&]$/, ""); + args[1][0] = url; + } + } + } + _channelCall(name, ...args); + }; +}); diff --git a/manifest.json b/manifest.json index bcbcc25..28ab5c5 100644 --- a/manifest.json +++ b/manifest.json @@ -6,7 +6,7 @@ "author_links": ["https://github.com/solstice23"], "description": "移除分享链接中的追踪参数", "preview": "preview.png", - "version": "1.0.2", + "version": "1.0.3", "ncm3-compatible": true, "requirements": [], "betterncm_version": ">=1.0.0",