-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing2.user.js
280 lines (249 loc) · 12.7 KB
/
testing2.user.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// ==UserScript==
// @name Testing 2
// @version 0.02.0006
// @description
// @include http://*
// ==/UserScript==
/*global console */
/*jshint expr:true, forin:true, noarg:true, noempty:true, eqeqeq:true, undef:true, curly:true, browser:true, jquery:true, laxcomma:true */
var outerCAA = { DEBUG : true
, ctrl : false
, key : 120
};
outerCAA.storeURL = function () {
'use strict';
var imageArray = JSON.parse(localStorage.getItem('Caabie_imageCache')),
newURL = window.location.hash.slice(1);
if (null === imageArray) {
imageArray = [];
}
newURL.length && imageArray.push(encodeURIComponent(newURL));
localStorage.setItem('Caabie_imageCache', JSON.stringify(imageArray));
outerCAA.DEBUG && console.log('Stored image url: ' + newURL);
return true;
};
outerCAA.sendMessage = function () {
'use strict';
var mbIframe = document.getElementById('mbIframe').contentWindow;
mbIframe.postMessage('MBstoredURL', '*');
console.log('Sent message from iframe to parent that an image has been stored.');
return;
};
outerCAA.MusicBrainz = function () {
'use strict';
document.location.pathname === '/doc/Cover_Art_Archive' && outerCAA.storeURL() && outerCAA.DEBUG && outerCAA.sendMessage();
return;
};
outerCAA.nonMB = function ($, DEBUG, ctrl, key) {
/* NOTE: We do not want strict mode here. It would cause the script to break if the site's code has modified object prototypes. */
/*jshint strict:false */
$.noConflict();
var innerCAA = {}
, $mbIframe = ''
, $highlights
, active = false
, src = 'http://musicbrainz.org/doc/Cover_Art_Archive#'
;
$.fn.enable = function (bool) {
return $(this).prop('disabled', !bool);
};
innerCAA.iframe = { create : function () {
DEBUG && console.log('Inserting iFrame.');
$mbIframe = $('<iframe id="mbIframe"/>').hide();
}
, sendURL : function (url) {
DEBUG && console.log('Passing image URL to the iFrame.');
var $newIframe = $mbIframe.clone()
.prop('src', src+url);
$('body').append($newIframe);
}
};
innerCAA.setListenerMBReturn = function () {
document.addEventListener('message', function (e) {
DEBUG && console.log('setListenerMBReturn:', e.data);
}, false);
};
innerCAA.addRule = function (selector, rule) {
return $('<style>').text(selector + rule)
.addClass('tinterCAA')
.appendTo($('body'));
};
innerCAA.tintImage = function (image) {
DEBUG && console.log('Tinting image');
var $image = $(image);
$image.wrap('<figure>')
.parent()
.addClass('tintWrapper')
.prop('title', 'Left click to send this image to CAA Batch Image Manager.');
return $image;
};
innerCAA.highlighter = {};
innerCAA.highlighter.init = function () {
DEBUG && console.log('Initializing the highlight css.');
var top = '.tintWrapper'
, img = '.tintWrapper > img'
, both = [top,img].join()
, addRule = function (ele, rule) {
return innerCAA.addRule(ele, '{' + rule + '}');
}
;
/* CSS reset */
addRule(top, 'margin: 0; padding: 0; outline: 0; vertical-align: baseline; display: inline-block;');
addRule(img, 'margin: 0; padding: 0; outline: 0;');
/* Rounded corners */
addRule(top, 'border-radius: 6px; -moz-border-radius: 6px; -webkit-border-radius: 6px;');
addRule(img, 'border-radius: 12px; -moz-border-radius: 12px; -webkit-border-radius: 12px;');
/* Tinted overlay */
addRule(top, 'background: rgba(45, 106, 210, .5);');
addRule(img, 'margin: 5px;');
var backlight = '0 0 22px rgba(0, 0, 200, .5);';
addRule(top, [ '-moz-box-shadow:', backlight, '-webkit-box-shadow:', backlight, 'box-shadow:', backlight].join(''));
/* Desaturation */
addRule(top, 'opacity: 0.9;');
addRule(img, 'opacity: 0.4;');
addRule(img, '-webkit-filter: grayscale(0.6);'); // Chrome
$('body').append('<svg xmlns="http://www.w3.org/2000/svg">' +
'<filter id="grayscale">' +
'<feColorMatrix type="matrix" values="0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0"/>' +
'</filter>' +
'</svg>'
);
addRule(img, 'filter: grayscale(#grayscale);'); // Firefox
$highlights = $('.tinterCAA');
$('body').on('mouseleave', 'img', function (e) {
DEBUG && console.log('Removing tinter wrap from image.');
var $e = $(e.target);
$e.parents('.tintWrapper:first').length && $e.removeClass('tintImage').unwrap();
}
);
};
innerCAA.highlighter.on = function () {
DEBUG && console.log('Enabling the highlight css.');
$highlights.enable(true);
$('body').on('mouseenter', 'img', function (e) {
DEBUG && console.log('Wrapping image with tinter.');
innerCAA.tintImage(e.target);
}
);
};
innerCAA.highlighter.off = function () {
DEBUG && console.log('Disabling the highlight css.');
$highlights.enable(false);
$('body').off('mouseenter', 'img');
};
innerCAA.keypress = { indicator: function (msg) {
$('#CAAIndicator').text(msg)
.show()
.delay(1200)
.fadeOut(500);
return;
}
, on : function () {
DEBUG && console.log('Activation keypress event -> on');
if (!$mbIframe.length) {
/* Status indicator */
innerCAA.addRule('#CAAIndicator', [ '{'
, 'color: #000;'
, 'background: white;'
, 'border: 2px solid teal;'
, '-webkit-border-radius: 0px 0px 0px 20px;'
, '-moz-border-radius: 0px 0px 0px 20px;'
, 'border-radius: 0px 0px 0px 20px;'
, 'font-size: 40px;'
, 'font-weight: 700;'
, 'padding: 10px;'
, 'position: fixed;'
, 'right: 0;'
, 'top: 0;'
, 'z-index: 10000;'
, '}'].join('\n'))
.removeClass('tinterCAA');
$('body').append($('<div id="CAAIndicator"/>').hide());
innerCAA.iframe.create();
innerCAA.highlighter.init();
DEBUG && innerCAA.setListenerMBReturn();
}
innerCAA.highlighter.on();
innerCAA.imageClick.on();
innerCAA.keypress.indicator('ON');
}
, off : function () {
DEBUG && console.log('Activation keypress event -> off');
innerCAA.imageClick.off();
innerCAA.highlighter.off();
innerCAA.keypress.indicator('OFF');
}
, toggle : function () {
DEBUG && console.log('Activation keypress event detected.');
innerCAA.keypress[(active = !active) ? 'on' : 'off']();
return;
}
, init : function () {
DEBUG && console.log('Initializing the activation keypress event.');
$(document).keydown(function (e) {
if (e.keyCode === key && (ctrl ? e.ctrlKey : true)) {
innerCAA.keypress.toggle();
}
});
}
};
innerCAA.imageClick = { on : function () {
DEBUG && console.log('Img click event -> on');
$('body').on('click', '.tintWrapper > img', function (e) {
e.preventDefault();
innerCAA.iframe.sendURL($(this).prop('src'));
return false;
});
}
, off : function () {
DEBUG && console.log('Img click event -> off');
$('body').off('click', '.tintWrapper > img');
}
};
innerCAA.activateEventListeners = function () {
innerCAA.keypress.init();
};
!function init () {
innerCAA.activateEventListeners();
}();
return;
};
outerCAA.loader = function (i) {
'use strict';
var script
, requires = ['http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js']
, body = document.getElementsByTagName('body')[0]
;
function makeScript() {
script = document.createElement('script');
script.type = 'text/javascript';
}
(function (x) {
makeScript();
script.src = requires[x];
script.addEventListener('load', function () {
if (++x !== requires.length) {
outerCAA.loader (x);
} else {
makeScript();
script.textContent = [ '(' , outerCAA.nonMB , ')(jQuery, ' ,
outerCAA.DEBUG , ',' ,
outerCAA.ctrl , ',' ,
outerCAA.key , ');'
].join('');
body.appendChild(script);
}
}, !0);
body.appendChild(script);
})(i || 0);
return;
};
!function init () {
'use strict';
outerCAA.DEBUG && console.log('Initializing CAA image linker script.');
if (document.location.host === 'musicbrainz.org') {
outerCAA.MusicBrainz();
} else {
outerCAA.loader(0);
}
}();