Browse Source

fix: add back tld rules to package

Gerald 8 years ago
parent
commit
0c082b0890
3 changed files with 39 additions and 35 deletions
  1. 3 3
      scripts/webpack.conf.js
  2. 3 32
      src/background/utils/tester.js
  3. 33 0
      src/background/utils/tld.js

+ 3 - 3
scripts/webpack.conf.js

@@ -52,9 +52,9 @@ targets.push(merge(base, {
     }),
     // new FriendlyErrorsPlugin(),
     !IS_DEV && new ExtractTextPlugin('[name].css'),
-    new webpack.NormalModuleReplacementPlugin(/\.\/rules\.json$/, resource => {
-      resource.request = path.resolve(__dirname, '../src/resources/empty-rules.json');
-    }),
+    // new webpack.NormalModuleReplacementPlugin(/\.\/rules\.json$/, resource => {
+    //   resource.request = path.resolve(__dirname, '../src/resources/empty-rules.json');
+    // }),
   ].filter(Boolean),
 }));
 

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

@@ -1,36 +1,7 @@
-import { fromUserSettings } from 'tldjs';
-import Trie from 'tldjs/lib/suffix-trie';
-import { request } from 'src/common';
+import tld from './tld';
 import cache from './cache';
 import { getOption, hookOptions } from './options';
 
-let tldjs;
-initTLD();
-
-function initTLD() {
-  // 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;
-    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');
-  });
-}
-
 const RE_MATCH_PARTS = /(.*?):\/\/([^/]*)\/(.*)/;
 let blacklistRules = [];
 hookOptions(changes => {
@@ -111,13 +82,13 @@ function autoReg(str) {
   const tests = [
     tstr => re.test(tstr),
   ];
-  if (tldjs && str.includes('.tld/')) {
+  if (tld && str.includes('.tld/')) {
     const reTldStr = reStr.replace('\\.tld/', '((?:\\.\\w+)+)/');
     tests.push(tstr => {
       const matches = tstr.match(reTldStr);
       if (matches) {
         const suffix = matches[1].slice(1);
-        if (tldjs.getPublicSuffix(suffix) === suffix) return true;
+        if (tld.getPublicSuffix(suffix) === suffix) return true;
       }
       return false;
     });

+ 33 - 0
src/background/utils/tld.js

@@ -0,0 +1,33 @@
+export default from 'tldjs';
+// import { fromUserSettings } from 'tldjs';
+// import Trie from 'tldjs/lib/suffix-trie';
+// import { request } from 'src/common';
+
+// let tldjs;
+// initTLD();
+//
+// function initTLD() {
+//   // 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;
+//     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 default tldjs