-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbookmarklet.html
101 lines (81 loc) · 2.6 KB
/
bookmarklet.html
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
<!DOCTYPE html>
<html>
<head>
<title>Bookmarklet Template</title>
<style>
* {
font-family: sans-serif;
}
body {
width: 600px;
margin: auto;
}
body h1 {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>Bookmarklet Template</h1>
<h2>Instructions</h2>
<p>
This bookmarklet is a tiny program that lives inside a browser bookmark.
You can add it to your browser like you'd add any ordinary bookmark.
And like a bookmark, click it to execute it.
</p>
<h2>The bookmarklet</h2>
<p>
Drag this to your bookmarks bar: <a class="bookmarklet">MyBookmarklet</a>
</p>
<script class="load-script-function" type="text">
function(url) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.async = true;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(script, s);
}
</script>
<script src="//ajax.microsoft.com/ajax/jquery/jquery-1.7.2.min.js"></script>
<script>
///////////////////////////////////////////////////////////////////
// Debug mode is for development only. Don't deploy it!
///////////////////////////////////////////////////////////////////
var DEBUG = false;
if (DEBUG) {
console.warn('NOTE!! You are in debug mode.');
}
// While not strictly necessary, having to deal with the page served as a file
// complicates the implementation past the point of it being worth it.
if (location.protocol === 'file:') {
var message = 'You can not run this as a file:// url. Please use a web server.';
alert(message);
throw new Error(message);
}
function makeBookmarklet($bookmarkletAnchor, bookmarkletFilename) {
// this assumes the js file is in the same folder as this file
var bookmarkletLocation = window.location.href.match(/.*\//)[0];
var url = bookmarkletLocation + bookmarkletFilename;
// get loadScript function and remove extra spaces
var loadScriptSrc = $('.load-script-function').html().replace(/\s\s+/g, ' ');
// warn users who try to click the bookmarklet unless in debug mode
$bookmarkletAnchor.click(function () {
if (DEBUG) {
return;
}
alert('To install, drag bookmarklet to your bookmarks bar.');
return false;
});
var template = 'javascript:({fn}("{url}?{rnd}"));';
$bookmarkletAnchor.attr('href', template
.replace('{fn}', loadScriptSrc)
.replace('{url}', url)
.replace('{rnd}', Math.random())); // cache bust
}
$(function () {
makeBookmarklet($('.bookmarklet'), 'bookmarklet.js');
});
</script>
</body>
</html>