diff options
-rw-r--r-- | _smooziee.js | 68 | ||||
-rw-r--r-- | asdfghjkl.js | 2 | ||||
-rw-r--r-- | pixiv.js | 49 |
3 files changed, 62 insertions, 57 deletions
diff --git a/_smooziee.js b/_smooziee.js index ae4915c..42eb9e3 100644 --- a/_smooziee.js +++ b/_smooziee.js @@ -80,68 +80,80 @@ var INFO = xml` let self = liberator.plugins.smooziee = (function(){ - // Mappings {{{ mappings.addUserMap( [modes.NORMAL], ["j"], "Smooth scroll down", function(count){ - self.smoothScrollBy(getScrollAmount() * (count || 1)); + self.smoothScrollBy(getScrollAmount()); }, { count: true } - ); + ); mappings.addUserMap( [modes.NORMAL], ["k"], "Smooth scroll up", function(count){ - self.smoothScrollBy(getScrollAmount() * -(count || 1)); + self.smoothScrollBy(getScrollAmount() * -1); }, { count: true } - ); - // }}} - // PUBLIC {{{ + ); + + var next; + var win; + var interval; + var PUBLICS = { smoothScrollBy: function(moment) { win = Buffer.findScrollableWindow(); - interval = window.eval(liberator.globalVariables.smooziee_scroll_interval || '20'); - destY = win.scrollY + moment; + interval = window.eval(liberator.globalVariables.smooth_scroll_interval || '30'); clearTimeout(next); smoothScroll(moment); } } - // }}} - // PRIVATE {{{ - var next; - var destY; - var win; - var interval; + function logBase(x, y) { + // Logarithm of arbitrary base `x` + return Math.log(y) / Math.log(x); + } - function getScrollAmount() window.eval(liberator.globalVariables.smooziee_scroll_amount || '400'); + function getScrollAmount() { + // see recognition of Fibonacci Numbers (here approximation is used) + // http://en.wikipedia.org/wiki/Fibonacci_number#Recognizing_Fibonacci_numbers + phi = 1.618033; + sqrt5 = 2.236067; + fn = liberator.globalVariables.smooth_scroll_amount || '150' + n = Math.ceil(logBase(phi, (fn * sqrt5 + Math.sqrt(5 * Math.pow(fn, 2) + 4)) / 2)) + return window.eval(n); + } - function smoothScroll(moment) { - if (moment > 0) - moment = Math.floor(moment / 2); - else - moment = Math.ceil(moment / 2); + function fib(n){ + // see optimized Binet's formula for Fibonacci sequence + // http://en.wikipedia.org/wiki/Fibonacci_number#Closed_form_expression + phi = 1.618033; + sqrt5 = 2.236067; + return Math.floor((Math.pow(phi, n) / sqrt5) + 0.5) + } - win.scrollBy(0, moment); + function smoothScroll(moment) { + if (moment > 0) { + moment = moment - 1; + win.scrollBy(0, fib(Math.abs(moment))); + } else { + moment = moment + 1; + win.scrollBy(0, -fib(Math.abs(moment))); + } - if (Math.abs(moment) < 1) { - setTimeout(makeScrollTo(win.scrollX, destY), interval); - destY = null; + if (moment == 0) return; - } + next = setTimeout(function() smoothScroll(moment), interval); } - function makeScrollTo(x, y) function() win.scrollTo(x, y); - // }}} return PUBLICS; })(); // vim: sw=2 ts=2 et si fdm=marker: diff --git a/asdfghjkl.js b/asdfghjkl.js index 1f48516..7826a48 100644 --- a/asdfghjkl.js +++ b/asdfghjkl.js @@ -98,7 +98,7 @@ let PLUGIN_INFO = xml` function around (obj, name, func) { let next = obj[name]; obj[name] = function () - let (self = this, args = arguments) + let (self = this, args = Array.from(arguments)) func.call(self, function () next.apply(self, args), args); @@ -212,22 +212,13 @@ commands.addUserCommand( // }}} // {{{ save single image file - let getImageUrl=function(pageContents){ - let url; - let htmldoc=getDOMHtmlDocument(pageContents); - if(htmldoc){ - if(0<htmldoc.getElementsByTagName('img').length) - url=htmldoc.getElementsByTagName('img').item(0).getAttribute('src'); - else - url=''; - }else{ - url=pageContents.match(/http:\/\/img[0-9]{2}\.pixiv\.net\/img\/[0-9a-z_]+\/[0-9]+\.jpg|http:\/\/img[0-9]{2}\.pixiv\.net\/img\/[0-9a-z_]+\/[0-9]+\.png/i); - } + let getImageUrl=function(pContents){ + let url = pContents.getElementsByClassName("_layout-thumbnail _illust_modal")[0].childNodes.item( 1 ).getAttribute( "data-src" ); return url; }; let saveImageFile=function(){ - let imgUrl=getImageUrl(xhrImgInfo.responseText); + let imgUrl=getImageUrl(contents); if(0<imgUrl.length){ let destPath=getDestPath(imgUrl); if(destPath==null){ @@ -278,9 +269,7 @@ commands.addUserCommand( // {{{ trueImgINfo let trueImgInfo=function(){ - if(-1!=type.search(/big&illust_id=/i)){ - saveImageFile(); - }else if(-1!=type.search(/manga&illust_id=/i)){ + if(-1!=type.search(/manga&illust_id=/i)){ saveMangaFiles(); }else{ liberator.echoerr("This page is not image page and not manga page."); @@ -301,16 +290,20 @@ commands.addUserCommand( done : function (aResult) { if ( aResult == fp.returnOK ) { saveDirectory = fp.file; - xhrImgInfo = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] - .createInstance(); - xhrImgInfo.QueryInterface( Ci.nsIDOMEventTarget ); - xhrImgInfo.addEventListener( "load", trueImgInfo, false ); - xhrImgInfo.addEventListener( "error", falseImgInfo, false ); - xhrImgInfo.QueryInterface( Ci.nsIXMLHttpRequest ); - xhrImgInfo.open( "GET", url, true); - xhrImgInfo.setRequestHeader( 'Referer', contents.URL ); - xhrImgInfo.setRequestHeader( 'Cookie', cookie ); - xhrImgInfo.send( null ); + if ( url == null ) { + saveImageFile(); + } else { + xhrImgInfo = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] + .createInstance(); + xhrImgInfo.QueryInterface( Ci.nsIDOMEventTarget ); + xhrImgInfo.addEventListener( "load", trueImgInfo, false ); + xhrImgInfo.addEventListener( "error", falseImgInfo, false ); + xhrImgInfo.QueryInterface( Ci.nsIXMLHttpRequest ); + xhrImgInfo.open( "GET", url, true); + xhrImgInfo.setRequestHeader( 'Referer', contents.URL ); + xhrImgInfo.setRequestHeader( 'Cookie', cookie ); + xhrImgInfo.send( null ); + } } } }; @@ -318,9 +311,9 @@ commands.addUserCommand( type = contents.getElementsByClassName('works_display') .item(0).firstChild.getAttribute('href'); - if(-1!=type.search(/big&illust_id=/i)){ - url=contents.documentURI.replace('medium','big'); - }else if(-1!=type.search(/manga&illust_id=/i)){ + if( type == null ){ // single iamge + url=null; + }else if(-1!=type.search(/manga&illust_id=/i)){ // plural image url=contents.documentURI.replace('medium','manga')+'&type=scroll'; }else{ liberator.echoerr("This page is not image page and not manga page."); |