Skip to content

Commit b320dd3

Browse files
committed
Merge pull request #53 from cobexer/bookmarklet
Bookmarklet
2 parents 8bbbace + 014c0ed commit b320dd3

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

bookmarklet.html

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>html2canvas Bookmarklet</title>
5+
<script type="text/javascript" src="external/jquery-1.6.2.min.js"></script>
6+
<script type="text/javascript">
7+
var isDebug = false, origBookmarklet = '';
8+
function patchLinks() {
9+
var bookmarklet = origBookmarklet;
10+
if (isDebug) {
11+
bookmarklet = bookmarklet.replace('//DEBUG: ', '');
12+
}
13+
bookmarklet = bookmarklet.replace(/\s\/\/.*/g, ''); // remove single line comments
14+
bookmarklet = bookmarklet.replace(/[\u000A\u000D]+/g, ''); // remove all linebreaks
15+
bookmarklet = bookmarklet.replace(/\/\*.*?\*\//g, ''); // remove multi line comments
16+
bookmarklet = bookmarklet.replace(/\s\s+/g, ' '); // reduce multiple spaces to single spaces
17+
bookmarklet = bookmarklet.replace(/\s+=\s+/g, '=');
18+
$('a.bookmarklet').each(function(_, el) {
19+
el.href = $(el).attr('data-href') + bookmarklet;
20+
});
21+
}
22+
$(function() {
23+
$('input[type=checkbox]').bind('change', function() {
24+
isDebug = $(this).is(':checked');
25+
patchLinks();
26+
}).change();
27+
$.ajax('src/plugins/bookmarklet.js', {
28+
dataType: 'text',
29+
success: function(data, status, xhr) {
30+
origBookmarklet = data;
31+
patchLinks();
32+
}
33+
});
34+
});
35+
</script>
36+
</head>
37+
<body>
38+
<h1>html2canvas Bookmarklet</h1>
39+
<p>
40+
If you use a normal browser: drag the normal <a class="bookmarklet" data-href="javascript:">html2canvas</a> bookmarklet to your bookmarks toolbar.<br />
41+
If not use the following link: <a class="bookmarklet" data-href="#_remove_this_javascript:">bookmarklet for those special mobile devices</a>
42+
click / tap that link and then bookmark the page, edit the bookmark and remove the start up until including <code>#_remove_this_</code>
43+
part at the beginning of the URL, it must start with: <code>javascript:</code> to be correct.
44+
</p>
45+
<p>
46+
If you are using Firefox and the NoScript Addon: disable the ABE part of it,
47+
took me quite some time to figure out that the reason for an unreliable bookmarklet was in NoScript...
48+
</p>
49+
<h2>For Developers:</h2>
50+
<p>
51+
If you are a developer and want to debug locally (you need the source tree of your html2canvas at:
52+
<code>http(s)://localhost/html2canvas/</code>)
53+
check the following box to get the bookmarklet patched automatically ;)<br />
54+
<label>Debug bookmarklet: <input type="checkbox" /></label>
55+
</p>
56+
</body>
57+
</html>

src/plugins/bookmarklet.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
(function() {
2+
/* options, customize to your needs */
3+
var server = '//html2canvas.hertzen.com/build',
4+
proxy = '//html2canvas.appspot.com',
5+
debug = false,
6+
profile = false;
7+
//DEBUG: server = 'http://localhost/html2canvas'; debug = true;
8+
var debugFiles = [
9+
'external/jquery-1.6.2.min',
10+
'src/Core',
11+
'src/Generate',
12+
'src/Parse',
13+
'src/Preload',
14+
'src/Queue',
15+
'src/Renderer',
16+
'src/plugins/jquery.plugin.html2canvas'
17+
],
18+
relFiles = [
19+
'//code.jquery.com/jquery-1.6.4.js',
20+
'html2canvas',
21+
'jquery.plugin.html2canvas'
22+
],
23+
i = 0, el = null;
24+
var loader = {
25+
index: 0,
26+
head: document.getElementsByTagName('head')[0],
27+
statusline: document.createElement('div'),
28+
files: (debug ? debugFiles : relFiles),
29+
onload: function () {
30+
var _ = this;
31+
if (_.index < _.files.length) {
32+
var el = document.createElement('script');
33+
el.type = 'text/javascript';
34+
el.onload = function() {
35+
_.onload();
36+
};
37+
el.onerror = function() {
38+
_.statusline.style.color = 'red';
39+
_.statusline.innerHTML = _.statusline.innerHTML + ' failed';
40+
_.statusline.onclick = function() {
41+
_.statusline.parentNode.removeChild(_.statusline);
42+
};
43+
};
44+
if (_.files[_.index].substr(0, 2) === '//') {
45+
el.src = _.files[_.index];
46+
}
47+
else {
48+
el.src = server + '/' + _.files[_.index] + '.js';
49+
}
50+
_.head.appendChild(el);
51+
++_.index;
52+
_.statusline.innerHTML = 'html2canvas: loading "' + el.src + '" ' + _.index + ' / ' + _.files.length + '...';
53+
}
54+
else {
55+
_.statusline.parentNode.removeChild(_.statusline);
56+
delete _.statusline;
57+
$(document.documentElement).html2canvas({
58+
logging: debug,
59+
profile: profile
60+
});
61+
}
62+
}
63+
}, statusline = loader.statusline;
64+
statusline.style.position = 'fixed';
65+
statusline.style.bottom = '0px';
66+
statusline.style.right = '20px';
67+
statusline.style.backgroundColor = 'white';
68+
statusline.style.border = '1px solid black';
69+
statusline.style.borderBottomWidth = '0px';
70+
statusline.style.padding = '2px 5px';
71+
statusline.style.zIndex = 9999999;
72+
document.body.appendChild(statusline);
73+
loader.onload();
74+
}());

0 commit comments

Comments
 (0)