This repository was archived by the owner on Jun 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbookmarklet.js
More file actions
161 lines (133 loc) · 3.06 KB
/
bookmarklet.js
File metadata and controls
161 lines (133 loc) · 3.06 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* ANS jQuery Bookmarklet launcher (v.3.0)
*
* A navalla suíza (http://idc.anavallasuiza.com/project/bookmarklet/)
*
* Released under the Creative Commons Attribution 3.0 Unported License,
* as defined here: http://creativecommons.org/licenses/by/3.0/
*/
"use strict";
window.bookmarklet = {
css: {},
js: {},
jQuery: false,
launch: function (file) {
if (!file) {
return false;
}
this.loadJS(file, function () {
var options = window.bookmarklet.options || {};
window.bookmarklet.execute(options);
});
},
execute: function (options) {
if (typeof(options.css) !== 'object') {
if (options.css) {
options.css = [options.css];
} else {
options.css = [];
}
}
if (typeof(options.js) !== 'object') {
if (options.js) {
options.js = [options.js];
} else {
options.js = [];
}
}
//Load css
if (options.css.length) {
var i;
for (i in options.css) {
window.bookmarklet.loadCSS(options.css[i]);
}
}
//Load jQuery
if (options.jquery) {
options.js.unshift(options.jquery);
}
//Load js
window.bookmarklet.loadMultipleJS(options.js, function () {
if (options.jquery) {
if (!window.bookmarklet.jQuery) {
window.bookmarklet.jQuery = window.jQuery.noConflict(true);
}
window.bookmarklet.jQuery(options.ready);
} else {
options.ready();
}
});
},
loadMultipleJS: function (files, onload) {
if (files.length === 0) {
if (onload) {
onload();
}
return true;
}
this.loadJS(files.shift(), function () {
window.bookmarklet.loadMultipleJS(files, onload);
});
},
loadJS: function (file, onload) {
var element = this.loadedJS(file);
if (element) {
if (typeof onload === 'function') {
onload.call(element);
}
return false;
}
element = document.createElement('script');
element.type = 'text/javascript';
element.src = file;
if (!document.attachEvent) {
element.onload = onload;
} else if (typeof onload === 'function') {
element.onreadystatechange = function () {
if (element.readyState === 'complete' || element.readyState === 'loaded') {
onload.call(element);
element.onreadystatechange = null;
}
};
}
document.body.appendChild(element);
this.js[file] = element;
return element;
},
loadCSS: function (file) {
if (this.loadedCSS(file)) {
return false;
}
var element = document.createElement('link');
element.setAttribute('rel', 'stylesheet');
element.setAttribute('type', 'text/css');
element.setAttribute('href', file);
document.getElementsByTagName('head')[0].appendChild(element);
this.css[file] = element;
return element;
},
loadedJS: function (file) {
if (this.js[file]) {
return this.js[file];
}
return false;
},
loadedCSS: function (file) {
if (this.css[file]) {
return this.css[file];
}
return false;
},
die: function () {
var i;
for (i in this.js) {
this.js[i].parentNode.removeChild(this.js[i]);
}
for (i in this.css) {
this.css[i].parentNode.removeChild(this.css[i]);
}
this.js = {};
this.css = {};
this.jQuery = false;
}
};