-
Notifications
You must be signed in to change notification settings - Fork 0
/
kittens.js
64 lines (62 loc) · 1.88 KB
/
kittens.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
(function() {
var kittenUrl = "http://placekitten.com/",
extra = "";
// If always enabled for this host.
chrome.extension.sendRequest({method: "getOptions", hostname: window.location.hostname}, function(response) {
if(response.enabledHost){
findPotentialKittens(document);
document.addEventListener("DOMNodeInserted", function (e) {
findPotentialKittens(e.target);
}, false);
}else {
listenForKonamiCode(findPotentialKittens);
}
});
/**
* Listen for konamicode
*/
function listenForKonamiCode (callback) {
// Thanks to http://mattkirman.com/2009/05/11/how-to-recreate-the-konami-code-in-javascript/
if (window.addEventListener) {
// create the keys and konami variables
var keys = [],
konami = "38,38,40,40,37,39,37,39,66,65";
// bind the keydown event to the Konami function
window.addEventListener("keydown", function (e){
// push the keycode to the 'keys' array
keys.push(e.keyCode);
// and check to see if the user has entered the Konami code
if (keys.toString().indexOf(konami) >= 0) {
callback();
// and finally clean up the keys array
keys = [];
}
}, true);
}
}
/**
* Loop through all the images and replace them with kittens.
*/
function findPotentialKittens () {
if(document.getElementsByTagName){
var imgs = document.getElementsByTagName('img');
for(var i = 0; i < imgs.length; i++) {
makeKitten(imgs[i])();
imgs[i].onload = makeKitten(this); // To also change cached images
}
}
}
/**
* Makes image into kitten if image is big enough and isn't already a kitten.
*/
function makeKitten (img) {
console.log('executing1');
return function () {
console.log('executing2', img);
if (img.width > 5 && img.height > 5 && img.src.slice(0, kittenUrl.length) !== kittenUrl) {
img.src = kittenUrl+extra+img.width+"/"+img.height;
extra = extra === "" ? "g/" : "";
}
};
}
})();