var ScreenshotSaver = { <br>
lambda: null , // EventListener <br>
mixin: null , // our file opener <br>
pure: null , // screengrab's file opener <br>
answer: 42, // to check `this' validity <br>
} ;<br>
<br>
ScreenshotSaver.assert = function (bool) { <br>
if (!bool) { <br>
repl.print( 'assertion failed' );<br>
repl.print( arguments .callee.caller);<br>
} <br>
} ;<br>
<br>
ScreenshotSaver.save = function (url, fpath) { <br>
this ._install_onload(fpath);<br>
window .content. location .href = url;<br>
} ;<br>
<br>
ScreenshotSaver._install_onload = function (fpath) { <br>
this .assert( this .lambda === null );<br>
<br>
// Looks like hack, but I need valid `this' in _dump_screenshot <br>
// I need this.lambda to be precise. <br>
this .lambda = function (ev) { <br>
repl.print( 'ScreenshotSaver: some `load \' event catched...' );<br>
if (!ev.target.mIsBusy)<br>
ScreenshotSaver._dump_screenshot(fpath);<br>
} ;<br>
window .getBrowser().addEventListener( "load" , this .lambda, false );<br>
} ;<br>
<br>
ScreenshotSaver._uninstall_onload = function () { <br>
this .assert( this .lambda !== null );<br>
window .getBrowser().removeEventListener( "load" , this .lambda, false );<br>
this .lambda = null ;<br>
} ;<br>
<br>
ScreenshotSaver._dump_screenshot = function (fpath) { <br>
this .assert( this .answer === 42);<br>
this .assert( this .lambda !== null );<br>
<br>
repl.print( 'ScreenshotSaver._dump_screenshot starts' );<br>
/* Wanna deregister handler from itself? Here are some useless links: <br>
* http://code.activestate.com/recipes/576366/ <br>
* http://en.wikipedia.org/wiki/Fixed_point_combinator <br>
* eval:"repl.print(arguments.callee)" */ <br>
this ._uninstall_onload();<br>
<br>
this ._install_screengrab_mixin(fpath);<br>
Screengrab.grabCompleteDocument();<br>
this ._uninstall_screengrab_mixin();<br>
<br>
repl.print( 'ScreenshotSaver._dump_screenshot exits' );<br>
} ;<br>
<br>
ScreenshotSaver._install_screengrab_mixin = function (fpath) { <br>
this .assert( this .mixin === null && this .pure === null );<br>
<br>
this .mixin = function (defaultName) { <br>
var file = Components.classes [ "@mozilla.org/file/local;1" ] .<br>
createInstance(Components.interfaces.nsILocalFile);<br>
file.initWithPath(fpath);<br>
return file;<br>
} <br>
<br>
this .pure = SGNsUtils.askUserForFile;<br>
SGNsUtils.askUserForFile = this .mixin;<br>
} ;<br>
<br>
ScreenshotSaver._uninstall_screengrab_mixin = function () { <br>
this .assert(SGNsUtils.askUserForFile === this .mixin);<br>
this .assert( this .mixin !== null && this .pure !== null );<br>
<br>
SGNsUtils.askUserForFile = this .pure;<br>
this .pure = null ;<br>
this .mixin = null ;<br>
} ;<br>
$ firefox & $ telnet localhost 4242 # 4242 - the port on which mozrepl is listening repl> repl.load ('file: ///home/luser/ScreenshotSaver.js'); repl> ScreenshotSaver.save ('http://habrahabr.ru', '/tmp/habr.png'); repl> ScreenshotSaver.save ('http://linux.org.ru', '/tmp/lor.png');
Source: https://habr.com/ru/post/49581/
All Articles