Browse Source

refactor: include tldjs in background chunk

...to simplify our code and production code (no webpackJsonpCallback chunk loader)
+ use `browser` as a global (except `injected`) for uniformity
tophf 2 years ago
parent
commit
b55e96ba48
5 changed files with 8 additions and 54 deletions
  1. 1 2
      scripts/plaid.conf.js
  2. 3 3
      src/background/index.js
  3. 3 4
      src/background/utils/tester.js
  4. 0 44
      src/common/tld.js
  5. 1 1
      src/popup/index.js

+ 1 - 2
scripts/plaid.conf.js

@@ -42,7 +42,7 @@ exports.optimization = {
         name: 'common-ui',
         test: new RegExp([
           /\bsvg/,
-          'src/common/(?!zip|tld)',
+          'src/common/(?!zip)',
           'node_modules/@violentmonkey/shortcut',
           'node_modules/@?vue',
         ].map(re => re.source || re).join('|').replace(/\\?\//g, '[/\\\\]')),
@@ -53,7 +53,6 @@ exports.optimization = {
         ].includes(c.name),
       },
       ...splitVendor('codemirror'),
-      ...splitVendor('tldjs'),
     },
   },
 };

+ 3 - 3
src/background/index.js

@@ -1,8 +1,8 @@
-import browser from '@/common/browser';
+import '@/common/browser';
 import { getActiveTab, makePause, sendCmd } from '@/common';
 import { TIMEOUT_24HOURS, TIMEOUT_MAX } from '@/common/consts';
 import { deepCopy } from '@/common/object';
-import * as tld from '@/common/tld';
+import { getDomain } from 'tldjs/tld';
 import * as sync from './sync';
 import { addOwnCommands, addPublicCommands, commands } from './utils';
 import { getData, getSizes, checkRemove } from './utils/db';
@@ -43,7 +43,7 @@ addOwnCommands({
     return {
       tab,
       host,
-      domain: host && tld.getDomain(host) || host,
+      domain: host && getDomain(host) || host,
     };
   },
 });

+ 3 - 4
src/background/utils/tester.js

@@ -2,7 +2,7 @@
 import { getScriptPrettyUrl } from '@/common';
 import { BLACKLIST, BLACKLIST_ERRORS } from '@/common/consts';
 import initCache from '@/common/cache';
-import * as tld from '@/common/tld';
+import { getPublicSuffix } from 'tldjs/tld';
 import { postInitialize } from './init';
 import { addOwnCommands } from './message';
 import { getOption, hookOptions } from './options';
@@ -72,7 +72,6 @@ hookOptions((changes) => {
     if (res) throw res; // will be passed to the UI
   }
 });
-tld.initTLD(true);
 
 export class MatchTest {
   constructor(rule, scheme, httpMod, host, path) {
@@ -267,7 +266,7 @@ function matchTld(tstr) {
   const matches = tstr.match(this);
   const suffix = matches?.[1]?.slice(1).toLowerCase();
   // Must return a proper boolean
-  return !!suffix && tld.getPublicSuffix(suffix) === suffix;
+  return !!suffix && getPublicSuffix(suffix) === suffix;
 }
 
 function hostMatcher(rule) {
@@ -275,7 +274,7 @@ function hostMatcher(rule) {
   // *.example.com
   // www.google.*
   // www.google.tld
-  const isTld = rule.endsWith('.tld') && tld.isReady();
+  const isTld = rule.endsWith('.tld');
   let prefix = '';
   let base = rule;
   let suffix = '';

+ 0 - 44
src/common/tld.js

@@ -1,44 +0,0 @@
-import tldjs from 'tldjs/tld';
-// import { fromUserSettings } from 'tldjs';
-// import Trie from 'tldjs/lib/suffix-trie';
-// import { request } from '@/common';
-
-// let tldjs;
-
-// export function initTLD(remote) {
-//   // TLD rules are too large to be packed, download them at runtime.
-//   const url = 'https://violentmonkey.top/static/tld-rules.json';
-//   const key = 'dat:tldRules';
-//   browser.storage.local.get(key)
-//   .then(({ [key]: tldRules }) => {
-//     if (tldRules) return tldRules;
-//     if (!remote) return Promise.reject('ignore TLD');
-//     return request(url, { responseType: 'json' })
-//     .then(({ data: rules }) => {
-//       console.info('Downloaded public suffix data');
-//       return browser.storage.local.set({ [key]: rules })
-//       .then(() => rules);
-//     });
-//   })
-//   .then(tldRules => {
-//     console.info('Initialized TLD');
-//     tldjs = fromUserSettings({ rules: Trie.fromJson(tldRules) });
-//   })
-//   .catch(err => {
-//     if (process.env.DEBUG) console.error(err);
-//     console.info('Failed initializing TLD');
-//   });
-// }
-export function initTLD() {}
-
-function exportMethod(key) {
-  return (...args) => tldjs && tldjs[key](...args);
-}
-
-export function isReady() {
-  return !!tldjs;
-}
-
-export const getDomain = exportMethod('getDomain');
-export const getSubdomain = exportMethod('getSubdomain');
-export const getPublicSuffix = exportMethod('getPublicSuffix');

+ 1 - 1
src/popup/index.js

@@ -1,4 +1,4 @@
-import browser from '@/common/browser';
+import '@/common/browser';
 import { sendCmdDirectly } from '@/common';
 import handlers from '@/common/handlers';
 import { loadScriptIcon } from '@/common/load-script-icon';