-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
134 lines (110 loc) · 2.79 KB
/
background.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
var enabled = new Object();
var tabId = 0;
var states =
[
new Stop(),
new Play()
//new Once()
];
var updateTabs = function () {
var state = readState();
chrome.browserAction.setIcon({ path: states[state].icon() })
}
var toggleState = function () {
var state = readState();
state = (state + 1) % states.length;
enabled[tabId] = state;
updateTabs();
};
var readState = function () {
if (enabled[tabId] === null || enabled[tabId] === undefined)
{
return 0;
}
return enabled[tabId];
}
chrome.browserAction.onClicked.addListener(function () {
toggleState();
});
chrome.tabs.onActivated.addListener(function (activeInfo) {
tabId = activeInfo.tabId;
updateTabs();
});
chrome.webRequest.onBeforeRequest.addListener(
function(info) {
if (states[readState()].abort()) {
return {};
}
var response = {redirectUrl: info.url};
request(info.url, function(xhr) {
try {
var singleImageSrc = getSingleImage(xhr);
response = {redirectUrl: singleImageSrc};
} catch (e) {
console.log(e);
}
});
return response;
},
// filters
{
urls: [
"http://*/*.gif",
"https://*/*.gif",
"http://*/*.gif?*",
"https://*/*.gif?*",
"http://*/*.gif&*",
"https://*/*.gif&*"
],
types: ["image"]
},
// extraInfoSpec
["blocking"]
);
function request(url, callback) {
var xhr = new XMLHttpRequest;
xhr.open('GET', url, false);
//XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
xhr.overrideMimeType('text/plain; charset=x-user-defined');
// console.log("Sending XHR for " + url);
xhr.send(null);
if (200 <= xhr.status && xhr.status <= 300) {
callback && callback(xhr);
}
}
function getSingleImage(xhr) {
var type = xhr.getResponseHeader('Content-Type');
var body = xhr.responseText.replace(/[\u0100-\uffff]/g, function(c) {
return String.fromCharCode(c.charCodeAt(0) & 0xff);
});
if (type === 'image/gif' || type === 'text/plain; charset=x-user-defined') {
var parser = new GifParser(body);
var image = parser.parse();
if (image.Header === null
|| image.Image.length === 0) {
throw new Error('ERROR: gif is broken.');
}
//if (image.Image.length !== 0) {
//throw new Error('Image is not animated, throw so we dont interfer with other plugins...');
//}
var nonAnimatedGif = [
image.Header,
image.GraphicControlExtension.length !== 0 ? image.GraphicControlExtension[0] : "",
image.Image[0],
image.Tail
].join('');
var dataURL = 'data:image/gif;base64,' + btoa(nonAnimatedGif);
//console.log(dataURL);
return dataURL;
}
throw new Error('ERROR: Image is not an animatable format.');
}
var GifImage = function () {
this.Header = null;
this.ApplicationExtension = null;
this.CommentExtension = null;
this.Image = [];
this.GraphicControlExtension = [];
this.Tail = ";";
};
updateTabs();