1. Anton Afanasyev
  2. autopagerize-extended

Commits

Anton Afanasyev  committed 479b3e7

support for non-regex AutoPager url patterns
this also corrects broken regex AutoPager patterns
But not convinced that everything works correctly

  • Participants
  • Parent commits 9898629
  • Branches extended
  • Tags pre-release

Comments (0)

Files changed (1)

File autopagerize.user.js

View file
 // @exclude        http://b.hatena.ne.jp/*
 // @exclude        http://www.facebook.com/plugins/like.php*
 // @exclude        http://api.tweetmeme.com/button.js*
-// @version        0.1.01
+// @version        0.1.02
 // @updateURL      https://bitbucket.org/aasoft/autopagerize-extended/raw/tip/autopagerize.user.js
 // @icon           http://autopagerize.net/img/icons/icon_032.png
 // @grant          GM_getValue
 // ==/UserScript==
 
 
-// version: 0.1.01 2012-08-31T18:23:34+09:00
+// version: 0.1.02 2012-12-27T18:23:34+09:00
 //
 // this script based on AutoPagerize http://userscripts.org/scripts/show/8551 thanks to swdyh.
 //
 var clearCache = function() {
     GM_setValue('cacheInfo', '');
     saveEternalPausePreferences( {} );
-}
+};
 var getCache = function() {
     //try {
     //  return JSON.parse(GM_getValue('cacheInfo')) || {};
     //} catch (e) {
         return eval(GM_getValue('cacheInfo')) || {};
     //}
-}
+};
+var AutoPagerPLNS = typeof AutoPagerPLNS != "undefined"?AutoPagerPLNS:{};
+AutoPagerPLNS.SimpleRegExp = function (s) {
+    this.p = s.substr(0,s.indexOf("*")).toLowerCase();
+    this.test = function(str) {
+        return str && str.toLowerCase().indexOf(this.p)==0;
+    };
+};
+
+var AutoPagerPL = {
+    Utils: {
+        // Converts a pattern in this programs simple notation to a regular expression.
+        // thanks AdBlock! http://www.mozdev.org/source/browse/adblock/adblock/
+        _simpleRegex : /^[^*]+\*$/,
+        _isSimple : function (s)
+        {
+            return (s && this._simpleRegex.test(s));
+        },
+        _convertToRegExpStr : function( pattern ) {
+            var s = new String( pattern );
+            var res = new String("^");
+
+            for (var i = 0 ; i < s.length ; i++) {
+                switch(s[i]) {
+                    case '*' :
+                        res += ".*";
+                        break;
+
+                    case '.' :
+                    case '?' :
+                    case '^' :
+                    case '$' :
+                    case '+' :
+                    case '{' :
+                    case '[' :
+                    case '|' :
+                    case '(' :
+                    case ')' :
+                    case ']' :
+                    case '/' :
+                        res += "\\" + s[i];
+                        break;
+
+                    case '\\' :
+                        res += "\\\\";
+                        break;
+
+                    case ' ' :
+                        // Remove spaces from URLs.
+                        break;
+
+                    default :
+                        res += s[i];
+                        break;
+                }
+            }
+
+            return res + '$';
+        },
+        ConvertToRegExp: function( pattern ) {
+            if (this._isSimple( pattern ))
+                return new AutoPagerPLNS.SimpleRegExp( pattern );        
+            return new RegExp( this._convertToRegExpStr( pattern ), "i" );
+        },
+        CorrectRegExp: function( pattern ) {
+            var s = new String(pattern);
+            var res = new String("");
+            var escaped = false;
+            var c = '';
+            for (var i = 0 ; i < s.length ; i++) {
+                c = s[i];
+                if(c == '\\')
+                {
+                    escaped = !escaped;
+                    res += c;
+                }else
+                {
+                    if (c == '/' && !escaped)
+                    {
+                        res += "\\";
+                    }
+                    res+=c;
+                    escaped = false;
+                }
+            }
+            return new RegExp(res, "i");
+        }
+    }
+};
+
+var convertAutoPagerUrlSelectorToRegex = function( shouldBeRegex, urlPattern ) {
+    // the following logic taken directly from the AutoPager's autopager-util.js, and cleaned up
+    if ( !shouldBeRegex ) {
+        return AutoPagerPL.Utils.ConvertToRegExp( urlPattern );
+    }
+
+    try {
+        return new RegExp( urlPattern );
+    } catch(re) {
+        try {
+            return new RegExp( AutoPagerPL.Utils.CorrectRegExp( urlPattern ) );
+        } catch(e) {
+            // error creating regexp, try to use it as pattern
+            return AutoPagerPL.Utils.ConvertToRegExp( urlPattern );
+        }        
+    }
+};
 var pickMapFunction = function( anItem ) {
     var nullMapper = function() { return null; };
 
     var autopagerizeMapper = function(i) { return i.data; };
     var autopagerMapper = function(i) { 
-        // FIXME: add support for non-regex url filters
-        if( !i.r )
-            return null;
+        var regexUrlSelectorPattern = convertAutoPagerUrlSelectorToRegex( i.r, i.u );
 
         return {
-            "url":          i.u,
-            "nextLink":     i.n,
-            "pageElement":  i.x
+            _urlRegex:      regexUrlSelectorPattern,
+            urlFunc:        function( url ) {
+                return this._urlRegex.test( url );
+            },
+            nextLink:       i.n,
+            pageElement:    i.x
         };
     };