ScreenshotSaver js tool to ease website screenshot creation Copyright

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* ScreenshotSaver.js - tool to ease website screenshot creation.
* Copyright (C) 2008-2009 Leonid Evdokimov <leon@darkk.net.ru>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* Requires Firefox and following plugins:
*
* mozrepl
* <http://hyperstruct.net/projects/mozlab>
* <http://wiki.github.com/bard/mozrepl/home>
* Tested with version 2008122712
*
* Screen grab!
* <http://www.screengrab.org/>
* <https://addons.mozilla.org/en-US/firefox/addon/1146>
* Tested with version 0.95
*
*
* Example:
*
* $ firefox &
* $ telnet localhost 4242
* 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');
*/
var ScreenshotSaver = {
lambda: null, // EventListener
mixin: null, // our file opener
pure: null, // screengrab's file opener
answer: 42, // to check `this' validity
};
ScreenshotSaver.assert = function(bool) {
if (!bool) {
repl.print('assertion failed');
repl.print(arguments.callee.caller);
}
};
ScreenshotSaver.save = function(url, fpath) {
this._install_onload(fpath);
window.content.location.href = url;
};
ScreenshotSaver._install_onload = function(fpath) {
this.assert(this.lambda === null);
// Looks like hack, but I need valid `this' in _dump_screenshot
// I need this.lambda to be precise.
this.lambda = function(ev) {
repl.print('ScreenshotSaver: some `load\' event catched...');
if (!ev.target.mIsBusy)
ScreenshotSaver._dump_screenshot(fpath);
};
window.getBrowser().addEventListener("load", this.lambda, false);
};
ScreenshotSaver._uninstall_onload = function() {
this.assert(this.lambda !== null);
window.getBrowser().removeEventListener("load", this.lambda, false);
this.lambda = null;
};
ScreenshotSaver._dump_screenshot = function(fpath) {
this.assert(this.answer === 42);
this.assert(this.lambda !== null);
repl.print('ScreenshotSaver._dump_screenshot starts');
/* Wanna deregister handler from itself? Here are some useless links:
* http://code.activestate.com/recipes/576366/
* http://en.wikipedia.org/wiki/Fixed_point_combinator
* eval:"repl.print(arguments.callee)" */
this._uninstall_onload();
this._install_screengrab_mixin(fpath);
Screengrab.grabCompleteDocument();
this._uninstall_screengrab_mixin();
repl.print('ScreenshotSaver._dump_screenshot exits');
};
ScreenshotSaver._install_screengrab_mixin = function(fpath) {
this.assert(this.mixin === null && this.pure === null);
this.mixin = function(defaultName) {
var file = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(fpath);
return file;
}
this.pure = SGNsUtils.askUserForFile;
SGNsUtils.askUserForFile = this.mixin;
};
ScreenshotSaver._uninstall_screengrab_mixin = function() {
this.assert(SGNsUtils.askUserForFile === this.mixin);
this.assert(this.mixin !== null && this.pure !== null);
SGNsUtils.askUserForFile = this.pure;
this.pure = null;
this.mixin = null;
};