You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

abp.js 23 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /* eslint-disable */
  2. // Was generated by gfwlist2pac in precise mode
  3. // https://github.com/clowwindy/gfwlist2pac
  4. // 2019-10-06: More 'javascript' way to interaction with main program
  5. // 2019-02-08: Updated to support shadowsocks-windows user rules.
  6. var proxy = __PROXY__;
  7. var userrules = [];
  8. var rules = [];
  9. // convert to abp grammar
  10. for (var i = 0; i < __RULES__.length; i++) {
  11. var s = __RULES__[i];
  12. if (s.substring(0, 2) == "||") s += "^";
  13. rules.push(s);
  14. }
  15. for (var i = 0; i < __USERRULES__.length; i++) {
  16. var s = __USERRULES__[i];
  17. if (s.substring(0, 2) == "||") s += "^";
  18. userrules.push(s);
  19. }
  20. /*
  21. * This file is part of Adblock Plus <http://adblockplus.org/>,
  22. * Copyright (C) 2006-2014 Eyeo GmbH
  23. *
  24. * Adblock Plus is free software: you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License version 3 as
  26. * published by the Free Software Foundation.
  27. *
  28. * Adblock Plus is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
  35. */
  36. function createDict()
  37. {
  38. var result = {};
  39. result.__proto__ = null;
  40. return result;
  41. }
  42. function getOwnPropertyDescriptor(obj, key)
  43. {
  44. if (obj.hasOwnProperty(key))
  45. {
  46. return obj[key];
  47. }
  48. return null;
  49. }
  50. function extend(subclass, superclass, definition)
  51. {
  52. if (Object.__proto__)
  53. {
  54. definition.__proto__ = superclass.prototype;
  55. subclass.prototype = definition;
  56. }
  57. else
  58. {
  59. var tmpclass = function(){}, ret;
  60. tmpclass.prototype = superclass.prototype;
  61. subclass.prototype = new tmpclass();
  62. subclass.prototype.constructor = superclass;
  63. for (var i in definition)
  64. {
  65. if (definition.hasOwnProperty(i))
  66. {
  67. subclass.prototype[i] = definition[i];
  68. }
  69. }
  70. }
  71. }
  72. function Filter(text)
  73. {
  74. this.text = text;
  75. this.subscriptions = [];
  76. }
  77. Filter.prototype = {
  78. text: null,
  79. subscriptions: null,
  80. toString: function()
  81. {
  82. return this.text;
  83. }
  84. };
  85. Filter.knownFilters = createDict();
  86. Filter.elemhideRegExp = /^([^\/\*\|\@"!]*?)#(\@)?(?:([\w\-]+|\*)((?:\([\w\-]+(?:[$^*]?=[^\(\)"]*)?\))*)|#([^{}]+))$/;
  87. Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w\-]+(?:=[^,\s]+)?(?:,~?[\w\-]+(?:=[^,\s]+)?)*)?$/;
  88. Filter.optionsRegExp = /\$(~?[\w\-]+(?:=[^,\s]+)?(?:,~?[\w\-]+(?:=[^,\s]+)?)*)$/;
  89. Filter.fromText = function(text)
  90. {
  91. if (text in Filter.knownFilters)
  92. {
  93. return Filter.knownFilters[text];
  94. }
  95. var ret;
  96. if (text.charAt(0) == "!")
  97. {
  98. ret = new CommentFilter(text);
  99. }
  100. else
  101. {
  102. ret = RegExpFilter.fromText(text);
  103. }
  104. Filter.knownFilters[ret.text] = ret;
  105. return ret;
  106. };
  107. function InvalidFilter(text, reason)
  108. {
  109. Filter.call(this, text);
  110. this.reason = reason;
  111. }
  112. extend(InvalidFilter, Filter, {
  113. reason: null
  114. });
  115. function CommentFilter(text)
  116. {
  117. Filter.call(this, text);
  118. }
  119. extend(CommentFilter, Filter, {
  120. });
  121. function ActiveFilter(text, domains)
  122. {
  123. Filter.call(this, text);
  124. this.domainSource = domains;
  125. }
  126. extend(ActiveFilter, Filter, {
  127. domainSource: null,
  128. domainSeparator: null,
  129. ignoreTrailingDot: true,
  130. domainSourceIsUpperCase: false,
  131. getDomains: function()
  132. {
  133. var prop = getOwnPropertyDescriptor(this, "domains");
  134. if (prop)
  135. {
  136. return prop;
  137. }
  138. var domains = null;
  139. if (this.domainSource)
  140. {
  141. var source = this.domainSource;
  142. if (!this.domainSourceIsUpperCase)
  143. {
  144. source = source.toUpperCase();
  145. }
  146. var list = source.split(this.domainSeparator);
  147. if (list.length == 1 && (list[0]).charAt(0) != "~")
  148. {
  149. domains = createDict();
  150. domains[""] = false;
  151. if (this.ignoreTrailingDot)
  152. {
  153. list[0] = list[0].replace(/\.+$/, "");
  154. }
  155. domains[list[0]] = true;
  156. }
  157. else
  158. {
  159. var hasIncludes = false;
  160. for (var i = 0; i < list.length; i++)
  161. {
  162. var domain = list[i];
  163. if (this.ignoreTrailingDot)
  164. {
  165. domain = domain.replace(/\.+$/, "");
  166. }
  167. if (domain == "")
  168. {
  169. continue;
  170. }
  171. var include;
  172. if (domain.charAt(0) == "~")
  173. {
  174. include = false;
  175. domain = domain.substr(1);
  176. }
  177. else
  178. {
  179. include = true;
  180. hasIncludes = true;
  181. }
  182. if (!domains)
  183. {
  184. domains = createDict();
  185. }
  186. domains[domain] = include;
  187. }
  188. domains[""] = !hasIncludes;
  189. }
  190. this.domainSource = null;
  191. }
  192. return this.domains;
  193. },
  194. sitekeys: null,
  195. isActiveOnDomain: function(docDomain, sitekey)
  196. {
  197. if (this.getSitekeys() && (!sitekey || this.getSitekeys().indexOf(sitekey.toUpperCase()) < 0))
  198. {
  199. return false;
  200. }
  201. if (!this.getDomains())
  202. {
  203. return true;
  204. }
  205. if (!docDomain)
  206. {
  207. return this.getDomains()[""];
  208. }
  209. if (this.ignoreTrailingDot)
  210. {
  211. docDomain = docDomain.replace(/\.+$/, "");
  212. }
  213. docDomain = docDomain.toUpperCase();
  214. while (true)
  215. {
  216. if (docDomain in this.getDomains())
  217. {
  218. return this.domains[docDomain];
  219. }
  220. var nextDot = docDomain.indexOf(".");
  221. if (nextDot < 0)
  222. {
  223. break;
  224. }
  225. docDomain = docDomain.substr(nextDot + 1);
  226. }
  227. return this.domains[""];
  228. },
  229. isActiveOnlyOnDomain: function(docDomain)
  230. {
  231. if (!docDomain || !this.getDomains() || this.getDomains()[""])
  232. {
  233. return false;
  234. }
  235. if (this.ignoreTrailingDot)
  236. {
  237. docDomain = docDomain.replace(/\.+$/, "");
  238. }
  239. docDomain = docDomain.toUpperCase();
  240. for (var domain in this.getDomains())
  241. {
  242. if (this.domains[domain] && domain != docDomain && (domain.length <= docDomain.length || domain.indexOf("." + docDomain) != domain.length - docDomain.length - 1))
  243. {
  244. return false;
  245. }
  246. }
  247. return true;
  248. }
  249. });
  250. function RegExpFilter(text, regexpSource, contentType, matchCase, domains, thirdParty, sitekeys)
  251. {
  252. ActiveFilter.call(this, text, domains, sitekeys);
  253. if (contentType != null)
  254. {
  255. this.contentType = contentType;
  256. }
  257. if (matchCase)
  258. {
  259. this.matchCase = matchCase;
  260. }
  261. if (thirdParty != null)
  262. {
  263. this.thirdParty = thirdParty;
  264. }
  265. if (sitekeys != null)
  266. {
  267. this.sitekeySource = sitekeys;
  268. }
  269. if (regexpSource.length >= 2 && regexpSource.charAt(0) == "/" && regexpSource.charAt(regexpSource.length - 1) == "/")
  270. {
  271. var regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), this.matchCase ? "" : "i");
  272. this.regexp = regexp;
  273. }
  274. else
  275. {
  276. this.regexpSource = regexpSource;
  277. }
  278. }
  279. extend(RegExpFilter, ActiveFilter, {
  280. domainSourceIsUpperCase: true,
  281. length: 1,
  282. domainSeparator: "|",
  283. regexpSource: null,
  284. getRegexp: function()
  285. {
  286. var prop = getOwnPropertyDescriptor(this, "regexp");
  287. if (prop)
  288. {
  289. return prop;
  290. }
  291. var source = this.regexpSource.replace(/\*+/g, "*").replace(/\^\|$/, "^").replace(/\W/g, "\\$&").replace(/\\\*/g, ".*").replace(/\\\^/g, "(?:[\\x00-\\x24\\x26-\\x2C\\x2F\\x3A-\\x40\\x5B-\\x5E\\x60\\x7B-\\x7F]|$)").replace(/^\\\|\\\|/, "^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?").replace(/^\\\|/, "^").replace(/\\\|$/, "$").replace(/^(\.\*)/, "").replace(/(\.\*)$/, "");
  292. var regexp = new RegExp(source, this.matchCase ? "" : "i");
  293. this.regexp = regexp;
  294. return regexp;
  295. },
  296. contentType: 2147483647,
  297. matchCase: false,
  298. thirdParty: null,
  299. sitekeySource: null,
  300. getSitekeys: function()
  301. {
  302. var prop = getOwnPropertyDescriptor(this, "sitekeys");
  303. if (prop)
  304. {
  305. return prop;
  306. }
  307. var sitekeys = null;
  308. if (this.sitekeySource)
  309. {
  310. sitekeys = this.sitekeySource.split("|");
  311. this.sitekeySource = null;
  312. }
  313. this.sitekeys = sitekeys;
  314. return this.sitekeys;
  315. },
  316. matches: function(location, contentType, docDomain, thirdParty, sitekey)
  317. {
  318. if (this.getRegexp().test(location) && this.isActiveOnDomain(docDomain, sitekey))
  319. {
  320. return true;
  321. }
  322. return false;
  323. }
  324. });
  325. RegExpFilter.prototype["0"] = "#this";
  326. RegExpFilter.fromText = function(text)
  327. {
  328. var blocking = true;
  329. var origText = text;
  330. if (text.indexOf("@@") == 0)
  331. {
  332. blocking = false;
  333. text = text.substr(2);
  334. }
  335. var contentType = null;
  336. var matchCase = null;
  337. var domains = null;
  338. var sitekeys = null;
  339. var thirdParty = null;
  340. var collapse = null;
  341. var options;
  342. var match = text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null;
  343. if (match)
  344. {
  345. options = match[1].toUpperCase().split(",");
  346. text = match.input.substr(0, match.index);
  347. for (var _loopIndex6 = 0; _loopIndex6 < options.length; ++_loopIndex6)
  348. {
  349. var option = options[_loopIndex6];
  350. var value = null;
  351. var separatorIndex = option.indexOf("=");
  352. if (separatorIndex >= 0)
  353. {
  354. value = option.substr(separatorIndex + 1);
  355. option = option.substr(0, separatorIndex);
  356. }
  357. option = option.replace(/-/, "_");
  358. if (option in RegExpFilter.typeMap)
  359. {
  360. if (contentType == null)
  361. {
  362. contentType = 0;
  363. }
  364. contentType |= RegExpFilter.typeMap[option];
  365. }
  366. else if (option.charAt(0) == "~" && option.substr(1) in RegExpFilter.typeMap)
  367. {
  368. if (contentType == null)
  369. {
  370. contentType = RegExpFilter.prototype.contentType;
  371. }
  372. contentType &= ~RegExpFilter.typeMap[option.substr(1)];
  373. }
  374. else if (option == "MATCH_CASE")
  375. {
  376. matchCase = true;
  377. }
  378. else if (option == "~MATCH_CASE")
  379. {
  380. matchCase = false;
  381. }
  382. else if (option == "DOMAIN" && typeof value != "undefined")
  383. {
  384. domains = value;
  385. }
  386. else if (option == "THIRD_PARTY")
  387. {
  388. thirdParty = true;
  389. }
  390. else if (option == "~THIRD_PARTY")
  391. {
  392. thirdParty = false;
  393. }
  394. else if (option == "COLLAPSE")
  395. {
  396. collapse = true;
  397. }
  398. else if (option == "~COLLAPSE")
  399. {
  400. collapse = false;
  401. }
  402. else if (option == "SITEKEY" && typeof value != "undefined")
  403. {
  404. sitekeys = value;
  405. }
  406. else
  407. {
  408. return new InvalidFilter(origText, "Unknown option " + option.toLowerCase());
  409. }
  410. }
  411. }
  412. if (!blocking && (contentType == null || contentType & RegExpFilter.typeMap.DOCUMENT) && (!options || options.indexOf("DOCUMENT") < 0) && !/^\|?[\w\-]+:/.test(text))
  413. {
  414. if (contentType == null)
  415. {
  416. contentType = RegExpFilter.prototype.contentType;
  417. }
  418. contentType &= ~RegExpFilter.typeMap.DOCUMENT;
  419. }
  420. try
  421. {
  422. if (blocking)
  423. {
  424. return new BlockingFilter(origText, text, contentType, matchCase, domains, thirdParty, sitekeys, collapse);
  425. }
  426. else
  427. {
  428. return new WhitelistFilter(origText, text, contentType, matchCase, domains, thirdParty, sitekeys);
  429. }
  430. }
  431. catch (e)
  432. {
  433. return new InvalidFilter(origText, e);
  434. }
  435. };
  436. RegExpFilter.typeMap = {
  437. OTHER: 1,
  438. SCRIPT: 2,
  439. IMAGE: 4,
  440. STYLESHEET: 8,
  441. OBJECT: 16,
  442. SUBDOCUMENT: 32,
  443. DOCUMENT: 64,
  444. XBL: 1,
  445. PING: 1,
  446. XMLHTTPREQUEST: 2048,
  447. OBJECT_SUBREQUEST: 4096,
  448. DTD: 1,
  449. MEDIA: 16384,
  450. FONT: 32768,
  451. BACKGROUND: 4,
  452. POPUP: 268435456,
  453. ELEMHIDE: 1073741824
  454. };
  455. RegExpFilter.prototype.contentType &= ~ (RegExpFilter.typeMap.ELEMHIDE | RegExpFilter.typeMap.POPUP);
  456. function BlockingFilter(text, regexpSource, contentType, matchCase, domains, thirdParty, sitekeys, collapse)
  457. {
  458. RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, thirdParty, sitekeys);
  459. this.collapse = collapse;
  460. }
  461. extend(BlockingFilter, RegExpFilter, {
  462. collapse: null
  463. });
  464. function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, thirdParty, sitekeys)
  465. {
  466. RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, thirdParty, sitekeys);
  467. }
  468. extend(WhitelistFilter, RegExpFilter, {
  469. });
  470. function Matcher()
  471. {
  472. this.clear();
  473. }
  474. Matcher.prototype = {
  475. filterByKeyword: null,
  476. keywordByFilter: null,
  477. clear: function()
  478. {
  479. this.filterByKeyword = createDict();
  480. this.keywordByFilter = createDict();
  481. },
  482. add: function(filter)
  483. {
  484. if (filter.text in this.keywordByFilter)
  485. {
  486. return;
  487. }
  488. var keyword = this.findKeyword(filter);
  489. var oldEntry = this.filterByKeyword[keyword];
  490. if (typeof oldEntry == "undefined")
  491. {
  492. this.filterByKeyword[keyword] = filter;
  493. }
  494. else if (oldEntry.length == 1)
  495. {
  496. this.filterByKeyword[keyword] = [oldEntry, filter];
  497. }
  498. else
  499. {
  500. oldEntry.push(filter);
  501. }
  502. this.keywordByFilter[filter.text] = keyword;
  503. },
  504. remove: function(filter)
  505. {
  506. if (!(filter.text in this.keywordByFilter))
  507. {
  508. return;
  509. }
  510. var keyword = this.keywordByFilter[filter.text];
  511. var list = this.filterByKeyword[keyword];
  512. if (list.length <= 1)
  513. {
  514. delete this.filterByKeyword[keyword];
  515. }
  516. else
  517. {
  518. var index = list.indexOf(filter);
  519. if (index >= 0)
  520. {
  521. list.splice(index, 1);
  522. if (list.length == 1)
  523. {
  524. this.filterByKeyword[keyword] = list[0];
  525. }
  526. }
  527. }
  528. delete this.keywordByFilter[filter.text];
  529. },
  530. findKeyword: function(filter)
  531. {
  532. var result = "";
  533. var text = filter.text;
  534. if (Filter.regexpRegExp.test(text))
  535. {
  536. return result;
  537. }
  538. var match = Filter.optionsRegExp.exec(text);
  539. if (match)
  540. {
  541. text = match.input.substr(0, match.index);
  542. }
  543. if (text.substr(0, 2) == "@@")
  544. {
  545. text = text.substr(2);
  546. }
  547. var candidates = text.toLowerCase().match(/[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/g);
  548. if (!candidates)
  549. {
  550. return result;
  551. }
  552. var hash = this.filterByKeyword;
  553. var resultCount = 16777215;
  554. var resultLength = 0;
  555. for (var i = 0, l = candidates.length; i < l; i++)
  556. {
  557. var candidate = candidates[i].substr(1);
  558. var count = candidate in hash ? hash[candidate].length : 0;
  559. if (count < resultCount || count == resultCount && candidate.length > resultLength)
  560. {
  561. result = candidate;
  562. resultCount = count;
  563. resultLength = candidate.length;
  564. }
  565. }
  566. return result;
  567. },
  568. hasFilter: function(filter)
  569. {
  570. return filter.text in this.keywordByFilter;
  571. },
  572. getKeywordForFilter: function(filter)
  573. {
  574. if (filter.text in this.keywordByFilter)
  575. {
  576. return this.keywordByFilter[filter.text];
  577. }
  578. else
  579. {
  580. return null;
  581. }
  582. },
  583. _checkEntryMatch: function(keyword, location, contentType, docDomain, thirdParty, sitekey)
  584. {
  585. var list = this.filterByKeyword[keyword];
  586. for (var i = 0; i < list.length; i++)
  587. {
  588. var filter = list[i];
  589. if (filter == "#this")
  590. {
  591. filter = list;
  592. }
  593. if (filter.matches(location, contentType, docDomain, thirdParty, sitekey))
  594. {
  595. return filter;
  596. }
  597. }
  598. return null;
  599. },
  600. matchesAny: function(location, contentType, docDomain, thirdParty, sitekey)
  601. {
  602. var candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g);
  603. if (candidates === null)
  604. {
  605. candidates = [];
  606. }
  607. candidates.push("");
  608. for (var i = 0, l = candidates.length; i < l; i++)
  609. {
  610. var substr = candidates[i];
  611. if (substr in this.filterByKeyword)
  612. {
  613. var result = this._checkEntryMatch(substr, location, contentType, docDomain, thirdParty, sitekey);
  614. if (result)
  615. {
  616. return result;
  617. }
  618. }
  619. }
  620. return null;
  621. }
  622. };
  623. function CombinedMatcher()
  624. {
  625. this.blacklist = new Matcher();
  626. this.whitelist = new Matcher();
  627. this.resultCache = createDict();
  628. }
  629. CombinedMatcher.maxCacheEntries = 1000;
  630. CombinedMatcher.prototype = {
  631. blacklist: null,
  632. whitelist: null,
  633. resultCache: null,
  634. cacheEntries: 0,
  635. clear: function()
  636. {
  637. this.blacklist.clear();
  638. this.whitelist.clear();
  639. this.resultCache = createDict();
  640. this.cacheEntries = 0;
  641. },
  642. add: function(filter)
  643. {
  644. if (filter instanceof WhitelistFilter)
  645. {
  646. this.whitelist.add(filter);
  647. }
  648. else
  649. {
  650. this.blacklist.add(filter);
  651. }
  652. if (this.cacheEntries > 0)
  653. {
  654. this.resultCache = createDict();
  655. this.cacheEntries = 0;
  656. }
  657. },
  658. remove: function(filter)
  659. {
  660. if (filter instanceof WhitelistFilter)
  661. {
  662. this.whitelist.remove(filter);
  663. }
  664. else
  665. {
  666. this.blacklist.remove(filter);
  667. }
  668. if (this.cacheEntries > 0)
  669. {
  670. this.resultCache = createDict();
  671. this.cacheEntries = 0;
  672. }
  673. },
  674. findKeyword: function(filter)
  675. {
  676. if (filter instanceof WhitelistFilter)
  677. {
  678. return this.whitelist.findKeyword(filter);
  679. }
  680. else
  681. {
  682. return this.blacklist.findKeyword(filter);
  683. }
  684. },
  685. hasFilter: function(filter)
  686. {
  687. if (filter instanceof WhitelistFilter)
  688. {
  689. return this.whitelist.hasFilter(filter);
  690. }
  691. else
  692. {
  693. return this.blacklist.hasFilter(filter);
  694. }
  695. },
  696. getKeywordForFilter: function(filter)
  697. {
  698. if (filter instanceof WhitelistFilter)
  699. {
  700. return this.whitelist.getKeywordForFilter(filter);
  701. }
  702. else
  703. {
  704. return this.blacklist.getKeywordForFilter(filter);
  705. }
  706. },
  707. isSlowFilter: function(filter)
  708. {
  709. var matcher = filter instanceof WhitelistFilter ? this.whitelist : this.blacklist;
  710. if (matcher.hasFilter(filter))
  711. {
  712. return !matcher.getKeywordForFilter(filter);
  713. }
  714. else
  715. {
  716. return !matcher.findKeyword(filter);
  717. }
  718. },
  719. matchesAnyInternal: function(location, contentType, docDomain, thirdParty, sitekey)
  720. {
  721. var candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g);
  722. if (candidates === null)
  723. {
  724. candidates = [];
  725. }
  726. candidates.push("");
  727. var blacklistHit = null;
  728. for (var i = 0, l = candidates.length; i < l; i++)
  729. {
  730. var substr = candidates[i];
  731. if (substr in this.whitelist.filterByKeyword)
  732. {
  733. var result = this.whitelist._checkEntryMatch(substr, location, contentType, docDomain, thirdParty, sitekey);
  734. if (result)
  735. {
  736. return result;
  737. }
  738. }
  739. if (substr in this.blacklist.filterByKeyword && blacklistHit === null)
  740. {
  741. blacklistHit = this.blacklist._checkEntryMatch(substr, location, contentType, docDomain, thirdParty, sitekey);
  742. }
  743. }
  744. return blacklistHit;
  745. },
  746. matchesAny: function(location, docDomain)
  747. {
  748. var key = location + " " + docDomain + " ";
  749. if (key in this.resultCache)
  750. {
  751. return this.resultCache[key];
  752. }
  753. var result = this.matchesAnyInternal(location, 0, docDomain, null, null);
  754. if (this.cacheEntries >= CombinedMatcher.maxCacheEntries)
  755. {
  756. this.resultCache = createDict();
  757. this.cacheEntries = 0;
  758. }
  759. this.resultCache[key] = result;
  760. this.cacheEntries++;
  761. return result;
  762. }
  763. };
  764. var userrulesMatcher = new CombinedMatcher();
  765. var defaultMatcher = new CombinedMatcher();
  766. var direct = 'DIRECT;';
  767. for (var i = 0; i < userrules.length; i++) {
  768. userrulesMatcher.add(Filter.fromText(userrules[i]));
  769. }
  770. for (var i = 0; i < rules.length; i++) {
  771. defaultMatcher.add(Filter.fromText(rules[i]));
  772. }
  773. function FindProxyForURL(url, host) {
  774. if (userrulesMatcher.matchesAny(url, host) instanceof BlockingFilter) {
  775. return proxy;
  776. }
  777. if (userrulesMatcher.matchesAny(url, host) instanceof WhitelistFilter) {
  778. return direct;
  779. }
  780. // Hack for Geosite, it provides a whitelist...
  781. if (defaultMatcher.matchesAny(url, host) instanceof WhitelistFilter) {
  782. return direct;
  783. }
  784. if (defaultMatcher.matchesAny(url, host) instanceof BlockingFilter) {
  785. return proxy;
  786. }
  787. return direct;
  788. }