| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 | 208 |
| 209 /** | 209 /** |
| 210 * See Filter.serialize() | 210 * See Filter.serialize() |
| 211 */ | 211 */ |
| 212 serialize: function(buffer) {} | 212 serialize: function(buffer) {} |
| 213 }; | 213 }; |
| 214 | 214 |
| 215 /** | 215 /** |
| 216 * Abstract base class for filters that can get hits | 216 * Abstract base class for filters that can get hits |
| 217 * @param {String} text see Filter() | 217 * @param {String} text see Filter() |
| 218 * @param {String} domains (optional) Domains that the filter is restricted to separated by domainSeparator e.g. "foo.com|bar.com|~baz.com" | 218 * @param {String} [domains] Domains that the filter is restricted to separated by domainSeparator e.g. "foo.com|bar.com|~baz.com" |
| 219 * @constructor | 219 * @constructor |
| 220 * @augments Filter | 220 * @augments Filter |
| 221 */ | 221 */ |
| 222 function ActiveFilter(text, domains) | 222 function ActiveFilter(text, domains) |
| 223 { | 223 { |
| 224 Filter.call(this, text); | 224 Filter.call(this, text); |
| 225 | 225 |
| 226 this.domainSource = domains; | 226 this.domainSource = domains; |
| 227 } | 227 } |
| 228 exports.ActiveFilter = ActiveFilter; | 228 exports.ActiveFilter = ActiveFilter; |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 374 } | 374 } |
| 375 | 375 |
| 376 this.domainSource = null; | 376 this.domainSource = null; |
| 377 } | 377 } |
| 378 | 378 |
| 379 Object.defineProperty(this, "domains", {value: domains, enumerable: true}); | 379 Object.defineProperty(this, "domains", {value: domains, enumerable: true}); |
| 380 return this.domains; | 380 return this.domains; |
| 381 }, | 381 }, |
| 382 | 382 |
| 383 /** | 383 /** |
| 384 * Array containing public keys of websites that this filter should apply to | |
| 385 * @type Array of String | |
| 386 */ | |
| 387 sitekeys: null, | |
| 388 | |
| 389 /** | |
| 384 * Checks whether this filter is active on a domain. | 390 * Checks whether this filter is active on a domain. |
| 391 * @param {String} docDomain domain name of the document that loads the URL | |
| 392 * @param {String} [sitekey] public key provided by the document | |
| 393 * @return {Boolean} true in case of the filter being active | |
| 385 */ | 394 */ |
| 386 isActiveOnDomain: function(/**String*/ docDomain) /**Boolean*/ | 395 isActiveOnDomain: function(docDomain, sitekey) |
| 387 { | 396 { |
| 397 if (this.sitekeys && (!sitekey || this.sitekeys.indexOf(sitekey.toUpperCase( )) < 0)) | |
|
Wladimir Palant
2014/08/29 20:13:59
Ok, we have an issue here. This code works in the
Thomas Greiner
2014/09/01 09:51:46
Done.
| |
| 398 return false; | |
| 399 | |
| 388 // If no domains are set the rule matches everywhere | 400 // If no domains are set the rule matches everywhere |
| 389 if (!this.domains) | 401 if (!this.domains) |
| 390 return true; | 402 return true; |
| 391 | 403 |
| 392 // If the document has no host name, match only if the filter isn't restrict ed to specific domains | 404 // If the document has no host name, match only if the filter isn't restrict ed to specific domains |
| 393 if (!docDomain) | 405 if (!docDomain) |
| 394 return this.domains[""]; | 406 return this.domains[""]; |
| 395 | 407 |
| 396 if (this.ignoreTrailingDot) | 408 if (this.ignoreTrailingDot) |
| 397 docDomain = docDomain.replace(/\.+$/, ""); | 409 docDomain = docDomain.replace(/\.+$/, ""); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 444 if (this._lastHit) | 456 if (this._lastHit) |
| 445 buffer.push("lastHit=" + this._lastHit); | 457 buffer.push("lastHit=" + this._lastHit); |
| 446 } | 458 } |
| 447 } | 459 } |
| 448 }; | 460 }; |
| 449 | 461 |
| 450 /** | 462 /** |
| 451 * Abstract base class for RegExp-based filters | 463 * Abstract base class for RegExp-based filters |
| 452 * @param {String} text see Filter() | 464 * @param {String} text see Filter() |
| 453 * @param {String} regexpSource filter part that the regular expression should b e build from | 465 * @param {String} regexpSource filter part that the regular expression should b e build from |
| 454 * @param {Number} contentType (optional) Content types the filter applies to, combination of values from RegExpFilter.typeMap | 466 * @param {Number} [contentType] Content types the filter applies to, combinatio n of values from RegExpFilter.typeMap |
| 455 * @param {Boolean} matchCase (optional) Defines whether the filter should dis tinguish between lower and upper case letters | 467 * @param {Boolean} [matchCase] Defines whether the filter should distinguish be tween lower and upper case letters |
| 456 * @param {String} domains (optional) Domains that the filter is restricted to, e.g. "foo.com|bar.com|~baz.com" | 468 * @param {String} [domains] Domains that the filter is restricted to, e.g. "foo .com|bar.com|~baz.com" |
| 457 * @param {Boolean} thirdParty (optional) Defines whether the filter should app ly to third-party or first-party content only | 469 * @param {Boolean} [thirdParty] Defines whether the filter should apply to thir d-party or first-party content only |
| 470 * @param {String} [sitekeys] Public keys of websites that this filter should ap ply to | |
| 458 * @constructor | 471 * @constructor |
| 459 * @augments ActiveFilter | 472 * @augments ActiveFilter |
| 460 */ | 473 */ |
| 461 function RegExpFilter(text, regexpSource, contentType, matchCase, domains, third Party) | 474 function RegExpFilter(text, regexpSource, contentType, matchCase, domains, third Party, sitekeys) |
| 462 { | 475 { |
| 463 ActiveFilter.call(this, text, domains); | 476 ActiveFilter.call(this, text, domains, sitekeys); |
| 464 | 477 |
| 465 if (contentType != null) | 478 if (contentType != null) |
| 466 this.contentType = contentType; | 479 this.contentType = contentType; |
| 467 if (matchCase) | 480 if (matchCase) |
| 468 this.matchCase = matchCase; | 481 this.matchCase = matchCase; |
| 469 if (thirdParty != null) | 482 if (thirdParty != null) |
| 470 this.thirdParty = thirdParty; | 483 this.thirdParty = thirdParty; |
| 484 if (sitekeys != null) | |
| 485 this.sitekeySource = sitekeys; | |
| 471 | 486 |
| 472 if (regexpSource.length >= 2 && regexpSource[0] == "/" && regexpSource[regexpS ource.length - 1] == "/") | 487 if (regexpSource.length >= 2 && regexpSource[0] == "/" && regexpSource[regexpS ource.length - 1] == "/") |
| 473 { | 488 { |
| 474 // The filter is a regular expression - convert it immediately to catch synt ax errors | 489 // The filter is a regular expression - convert it immediately to catch synt ax errors |
| 475 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), thi s.matchCase ? "" : "i"); | 490 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), thi s.matchCase ? "" : "i"); |
| 476 Object.defineProperty(this, "regexp", {value: regexp}); | 491 Object.defineProperty(this, "regexp", {value: regexp}); |
| 477 } | 492 } |
| 478 else | 493 else |
| 479 { | 494 { |
| 480 // No need to convert this filter to regular expression yet, do it on demand | 495 // No need to convert this filter to regular expression yet, do it on demand |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 542 * @type Boolean | 557 * @type Boolean |
| 543 */ | 558 */ |
| 544 matchCase: false, | 559 matchCase: false, |
| 545 /** | 560 /** |
| 546 * Defines whether the filter should apply to third-party or first-party conte nt only. Can be null (apply to all content). | 561 * Defines whether the filter should apply to third-party or first-party conte nt only. Can be null (apply to all content). |
| 547 * @type Boolean | 562 * @type Boolean |
| 548 */ | 563 */ |
| 549 thirdParty: null, | 564 thirdParty: null, |
| 550 | 565 |
| 551 /** | 566 /** |
| 567 * String that the sitekey property should be generated from | |
| 568 * @type String | |
| 569 */ | |
| 570 sitekeySource: null, | |
| 571 | |
| 572 /** | |
| 573 * Array containing public keys of websites that this filter should apply to | |
| 574 * @type Array of String | |
| 575 */ | |
| 576 get sitekeys() | |
| 577 { | |
| 578 let sitekeys = null; | |
| 579 | |
| 580 if (this.sitekeySource) | |
| 581 { | |
| 582 sitekeys = this.sitekeySource.split("|"); | |
| 583 this.sitekeySource = null; | |
| 584 } | |
| 585 | |
| 586 Object.defineProperty(this, "sitekeys", {value: sitekeys, enumerable: true}) ; | |
| 587 return this.sitekeys; | |
| 588 }, | |
| 589 | |
| 590 /** | |
| 552 * Tests whether the URL matches this filter | 591 * Tests whether the URL matches this filter |
| 553 * @param {String} location URL to be tested | 592 * @param {String} location URL to be tested |
| 554 * @param {String} contentType content type identifier of the URL | 593 * @param {String} contentType content type identifier of the URL |
| 555 * @param {String} docDomain domain name of the document that loads the URL | 594 * @param {String} docDomain domain name of the document that loads the URL |
| 556 * @param {Boolean} thirdParty should be true if the URL is a third-party requ est | 595 * @param {Boolean} thirdParty should be true if the URL is a third-party requ est |
| 596 * @param {String} sitekey public key provided by the document | |
| 557 * @return {Boolean} true in case of a match | 597 * @return {Boolean} true in case of a match |
| 558 */ | 598 */ |
| 559 matches: function(location, contentType, docDomain, thirdParty) | 599 matches: function(location, contentType, docDomain, thirdParty, sitekey) |
| 560 { | 600 { |
| 561 if (this.regexp.test(location) && | 601 if (this.regexp.test(location) && |
| 562 (RegExpFilter.typeMap[contentType] & this.contentType) != 0 && | 602 (RegExpFilter.typeMap[contentType] & this.contentType) != 0 && |
| 563 (this.thirdParty == null || this.thirdParty == thirdParty) && | 603 (this.thirdParty == null || this.thirdParty == thirdParty) && |
| 564 this.isActiveOnDomain(docDomain)) | 604 this.isActiveOnDomain(docDomain, sitekey)) |
| 565 { | 605 { |
| 566 return true; | 606 return true; |
| 567 } | 607 } |
| 568 | 608 |
| 569 return false; | 609 return false; |
| 570 } | 610 } |
| 571 }; | 611 }; |
| 572 | 612 |
| 573 // Required to optimize Matcher, see also RegExpFilter.prototype.length | 613 // Required to optimize Matcher, see also RegExpFilter.prototype.length |
| 574 Object.defineProperty(RegExpFilter.prototype, "0", | 614 Object.defineProperty(RegExpFilter.prototype, "0", |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 586 let origText = text; | 626 let origText = text; |
| 587 if (text.indexOf("@@") == 0) | 627 if (text.indexOf("@@") == 0) |
| 588 { | 628 { |
| 589 blocking = false; | 629 blocking = false; |
| 590 text = text.substr(2); | 630 text = text.substr(2); |
| 591 } | 631 } |
| 592 | 632 |
| 593 let contentType = null; | 633 let contentType = null; |
| 594 let matchCase = null; | 634 let matchCase = null; |
| 595 let domains = null; | 635 let domains = null; |
| 596 let siteKeys = null; | 636 let sitekeys = null; |
| 597 let thirdParty = null; | 637 let thirdParty = null; |
| 598 let collapse = null; | 638 let collapse = null; |
| 599 let options; | 639 let options; |
| 600 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); | 640 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); |
| 601 if (match) | 641 if (match) |
| 602 { | 642 { |
| 603 options = match[1].toUpperCase().split(","); | 643 options = match[1].toUpperCase().split(","); |
| 604 text = match.input.substr(0, match.index); | 644 text = match.input.substr(0, match.index); |
| 605 for (let option of options) | 645 for (let option of options) |
| 606 { | 646 { |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 632 domains = value; | 672 domains = value; |
| 633 else if (option == "THIRD_PARTY") | 673 else if (option == "THIRD_PARTY") |
| 634 thirdParty = true; | 674 thirdParty = true; |
| 635 else if (option == "~THIRD_PARTY") | 675 else if (option == "~THIRD_PARTY") |
| 636 thirdParty = false; | 676 thirdParty = false; |
| 637 else if (option == "COLLAPSE") | 677 else if (option == "COLLAPSE") |
| 638 collapse = true; | 678 collapse = true; |
| 639 else if (option == "~COLLAPSE") | 679 else if (option == "~COLLAPSE") |
| 640 collapse = false; | 680 collapse = false; |
| 641 else if (option == "SITEKEY" && typeof value != "undefined") | 681 else if (option == "SITEKEY" && typeof value != "undefined") |
| 642 siteKeys = value.split(/\|/); | 682 sitekeys = value; |
| 643 else | 683 else |
| 644 return new InvalidFilter(origText, "Unknown option " + option.toLowerCas e()); | 684 return new InvalidFilter(origText, "Unknown option " + option.toLowerCas e()); |
| 645 } | 685 } |
| 646 } | 686 } |
| 647 | 687 |
| 648 if (!blocking && (contentType == null || (contentType & RegExpFilter.typeMap.D OCUMENT)) && | 688 if (!blocking && (contentType == null || (contentType & RegExpFilter.typeMap.D OCUMENT)) && |
| 649 (!options || options.indexOf("DOCUMENT") < 0) && !/^\|?[\w\-]+:/.test(text )) | 689 (!options || options.indexOf("DOCUMENT") < 0) && !/^\|?[\w\-]+:/.test(text )) |
| 650 { | 690 { |
| 651 // Exception filters shouldn't apply to pages by default unless they start w ith a protocol name | 691 // Exception filters shouldn't apply to pages by default unless they start w ith a protocol name |
| 652 if (contentType == null) | 692 if (contentType == null) |
| 653 contentType = RegExpFilter.prototype.contentType; | 693 contentType = RegExpFilter.prototype.contentType; |
| 654 contentType &= ~RegExpFilter.typeMap.DOCUMENT; | 694 contentType &= ~RegExpFilter.typeMap.DOCUMENT; |
| 655 } | 695 } |
| 656 if (!blocking && siteKeys) | |
| 657 contentType = RegExpFilter.typeMap.DOCUMENT; | |
| 658 | 696 |
| 659 try | 697 try |
| 660 { | 698 { |
| 661 if (blocking) | 699 if (blocking) |
| 662 return new BlockingFilter(origText, text, contentType, matchCase, domains, thirdParty, collapse); | 700 return new BlockingFilter(origText, text, contentType, matchCase, domains, thirdParty, sitekeys, collapse); |
| 663 else | 701 else |
| 664 return new WhitelistFilter(origText, text, contentType, matchCase, domains , thirdParty, siteKeys); | 702 return new WhitelistFilter(origText, text, contentType, matchCase, domains , thirdParty, sitekeys); |
| 665 } | 703 } |
| 666 catch (e) | 704 catch (e) |
| 667 { | 705 { |
| 668 return new InvalidFilter(origText, e); | 706 return new InvalidFilter(origText, e); |
| 669 } | 707 } |
| 670 } | 708 } |
| 671 | 709 |
| 672 /** | 710 /** |
| 673 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks | 711 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks |
| 674 */ | 712 */ |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 698 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.ELEMHIDE | RegExpFi lter.typeMap.POPUP); | 736 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.ELEMHIDE | RegExpFi lter.typeMap.POPUP); |
| 699 | 737 |
| 700 /** | 738 /** |
| 701 * Class for blocking filters | 739 * Class for blocking filters |
| 702 * @param {String} text see Filter() | 740 * @param {String} text see Filter() |
| 703 * @param {String} regexpSource see RegExpFilter() | 741 * @param {String} regexpSource see RegExpFilter() |
| 704 * @param {Number} contentType see RegExpFilter() | 742 * @param {Number} contentType see RegExpFilter() |
| 705 * @param {Boolean} matchCase see RegExpFilter() | 743 * @param {Boolean} matchCase see RegExpFilter() |
| 706 * @param {String} domains see RegExpFilter() | 744 * @param {String} domains see RegExpFilter() |
| 707 * @param {Boolean} thirdParty see RegExpFilter() | 745 * @param {Boolean} thirdParty see RegExpFilter() |
| 746 * @param {String} sitekeys see RegExpFilter() | |
| 708 * @param {Boolean} collapse defines whether the filter should collapse blocked content, can be null | 747 * @param {Boolean} collapse defines whether the filter should collapse blocked content, can be null |
| 709 * @constructor | 748 * @constructor |
| 710 * @augments RegExpFilter | 749 * @augments RegExpFilter |
| 711 */ | 750 */ |
| 712 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, thi rdParty, collapse) | 751 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, thi rdParty, sitekeys, collapse) |
| 713 { | 752 { |
| 714 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, t hirdParty); | 753 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, t hirdParty, sitekeys); |
| 715 | 754 |
| 716 this.collapse = collapse; | 755 this.collapse = collapse; |
| 717 } | 756 } |
| 718 exports.BlockingFilter = BlockingFilter; | 757 exports.BlockingFilter = BlockingFilter; |
| 719 | 758 |
| 720 BlockingFilter.prototype = | 759 BlockingFilter.prototype = |
| 721 { | 760 { |
| 722 __proto__: RegExpFilter.prototype, | 761 __proto__: RegExpFilter.prototype, |
| 723 | 762 |
| 724 /** | 763 /** |
| 725 * Defines whether the filter should collapse blocked content. Can be null (us e the global preference). | 764 * Defines whether the filter should collapse blocked content. Can be null (us e the global preference). |
| 726 * @type Boolean | 765 * @type Boolean |
| 727 */ | 766 */ |
| 728 collapse: null | 767 collapse: null |
| 729 }; | 768 }; |
| 730 | 769 |
| 731 /** | 770 /** |
| 732 * Class for whitelist filters | 771 * Class for whitelist filters |
| 733 * @param {String} text see Filter() | 772 * @param {String} text see Filter() |
| 734 * @param {String} regexpSource see RegExpFilter() | 773 * @param {String} regexpSource see RegExpFilter() |
| 735 * @param {Number} contentType see RegExpFilter() | 774 * @param {Number} contentType see RegExpFilter() |
| 736 * @param {Boolean} matchCase see RegExpFilter() | 775 * @param {Boolean} matchCase see RegExpFilter() |
| 737 * @param {String} domains see RegExpFilter() | 776 * @param {String} domains see RegExpFilter() |
| 738 * @param {Boolean} thirdParty see RegExpFilter() | 777 * @param {Boolean} thirdParty see RegExpFilter() |
| 739 * @param {String[]} siteKeys public keys of websites that this filter should ap ply to | 778 * @param {String} sitekeys see RegExpFilter() |
| 740 * @constructor | 779 * @constructor |
| 741 * @augments RegExpFilter | 780 * @augments RegExpFilter |
| 742 */ | 781 */ |
| 743 function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, th irdParty, siteKeys) | 782 function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, th irdParty, sitekeys) |
| 744 { | 783 { |
| 745 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, t hirdParty); | 784 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, t hirdParty, sitekeys); |
| 746 | |
| 747 if (siteKeys != null) | |
| 748 this.siteKeys = siteKeys; | |
| 749 } | 785 } |
| 750 exports.WhitelistFilter = WhitelistFilter; | 786 exports.WhitelistFilter = WhitelistFilter; |
| 751 | 787 |
| 752 WhitelistFilter.prototype = | 788 WhitelistFilter.prototype = |
| 753 { | 789 { |
| 754 __proto__: RegExpFilter.prototype, | 790 __proto__: RegExpFilter.prototype |
| 755 | |
| 756 /** | |
| 757 * List of public keys of websites that this filter should apply to | |
| 758 * @type String[] | |
| 759 */ | |
| 760 siteKeys: null | |
| 761 } | 791 } |
| 762 | 792 |
| 763 /** | 793 /** |
| 764 * Base class for element hiding filters | 794 * Base class for element hiding filters |
| 765 * @param {String} text see Filter() | 795 * @param {String} text see Filter() |
| 766 * @param {String} domains (optional) Host names or domains the filter should be restricted to | 796 * @param {String} [domains] Host names or domains the filter should be restrict ed to |
| 767 * @param {String} selector CSS selector for the HTML elements that should be hidden | 797 * @param {String} selector CSS selector for the HTML elements that should be hidden |
| 768 * @constructor | 798 * @constructor |
| 769 * @augments ActiveFilter | 799 * @augments ActiveFilter |
| 770 */ | 800 */ |
| 771 function ElemHideBase(text, domains, selector) | 801 function ElemHideBase(text, domains, selector) |
| 772 { | 802 { |
| 773 ActiveFilter.call(this, text, domains || null); | 803 ActiveFilter.call(this, text, domains || null); |
| 774 | 804 |
| 775 if (domains) | 805 if (domains) |
| 776 this.selectorDomain = domains.replace(/,~[^,]+/g, "").replace(/^~[^,]+,?/, " ").toLowerCase(); | 806 this.selectorDomain = domains.replace(/,~[^,]+/g, "").replace(/^~[^,]+,?/, " ").toLowerCase(); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 890 function ElemHideException(text, domains, selector) | 920 function ElemHideException(text, domains, selector) |
| 891 { | 921 { |
| 892 ElemHideBase.call(this, text, domains, selector); | 922 ElemHideBase.call(this, text, domains, selector); |
| 893 } | 923 } |
| 894 exports.ElemHideException = ElemHideException; | 924 exports.ElemHideException = ElemHideException; |
| 895 | 925 |
| 896 ElemHideException.prototype = | 926 ElemHideException.prototype = |
| 897 { | 927 { |
| 898 __proto__: ElemHideBase.prototype | 928 __proto__: ElemHideBase.prototype |
| 899 }; | 929 }; |
| OLD | NEW |