This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
webp_reformatter.js
283 lines (268 loc) · 9.51 KB
/
webp_reformatter.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
281
282
283
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Script that analyzes the DOM and makes the WebP images visible.
//
// Author: Vincent Rabaud ([email protected])
/** Port that maintains connection with the background script */
var PORT = chrome.runtime.connect({name: "webp"});
/** Set of WebP types displayed in the current iframe. */
var DISPLAYED_WEBP_TYPES = new Set();
/** Time of the last test for WebP on the page, initially a long time ago. */
var TEST_TIME = (new Date(2010)).getTime();
/** Object linking a URL to a list of jQuery objects */
var URL_TO_IMG = {};
/**
* Re-style an img/div in the DOM by changing the CSS.
* It also adds a title which contains info. Works on simple sites like
* https://developers.google.com/speed/webp/gallery
* Not on complex sites like Netflix.
* TODO: deal with animations
* @param {Object} obj the jQuery DOM object.
* @param {string} url the URL of the resource.
* @param {integer} quality if negative, lossless, otherwise the lossy quality.
* @param {string} type one of "VP8 " or "VP8L".
*/
function ChangeWebPImage(obj, url, quality, type) {
// Log data.
if (type == "VP8 ") {
if (quality < 0) quality = 0;
} else if (type != "VP8L" && type != "VP8X") {
return;
}
// Check whether the image has already been analyzed.
var title = $(obj).attr("title");
if (title == undefined) title = "";
if (title.includes("WebP loss") || title.includes("WebP extended")) return;
var types_size_old = DISPLAYED_WEBP_TYPES.size;
DISPLAYED_WEBP_TYPES.add(type);
if (DISPLAYED_WEBP_TYPES.size > types_size_old) {
// We have a new WebP type, update the HTML header.
UpdateIFrameHeader(DISPLAYED_WEBP_TYPES);
// Notify other potential iframes if a new type appeared.
PORT.postMessage({
"request": "webp_set_tab_types",
"tab_types": Array.from(DISPLAYED_WEBP_TYPES)
});
}
// Change the CSS.
var color = "#00FF00";
if (type == "VP8L") {
color = "#FF00FF";
} else if (type == "VP8X") {
color = "#FF0000";
}
$(obj).css({
"border-color": color,
"border-width": "20px",
"border-style": "dashed",
"box-sizing": "border-box"
});
title += "\n";
if (type == "VP8 ") {
title += "WebP lossy, quality ~" + quality;
} else if (type == "VP8L") {
title += "WebP lossless";
} else if (type == "VP8X") {
title += "WebP extended, quality ~" + quality;
}
$(obj).attr("title", title + ", " + url);
// Log the findings in the current developer log console.
if (type == "VP8 ") {
console.log("Lossy WebP, quality ~" + quality + ", " + url);
} else if (type == "VP8L") {
console.log("Lossless WebP, " + url);
} else if (type == "VP8X") {
console.log("Lossy WebP Extended, quality ~" + quality + ", " + url);
}
}
/**
* Update the header of the iframe to display the types of WebP used if any.
* @param {Array.<string>} types Array of WebP types to display a summary of.
*/
function UpdateIFrameHeader(types) {
if (types.length == 0) {
return;
}
chrome.storage.local.get("do_display_summary", function(data) {
if (!data["do_display_summary"]) return;
// Display a big banner on top.
var div = $("div#webp_div_on_top");
if (div.length == 0) {
$("body").prepend('<div id="webp_div_on_top"/>');
}
div = $("div#webp_div_on_top").first();
var types_set = new Set(div.text().split(", "));
for(var type of types) {
if (type == "VP8 ") {
types_set.add("lossy");
} else if (type == "VP8L") {
types_set.add("lossless");
} else if (type == "VP8X") {
types_set.add("extended");
}
}
types_set.delete("");
div.text(Array.from(types_set).join(", "));
// z-index is at 1 billion on YT so I had to go further ...
div.css("position", "fixed").css("z-index", 100000000000)
.css("background", "red").css("font-size", "40px")
.css("margin-left", "40%");
});
}
// -----------------------------------------------------------------------------
/**
* Sends the url to check to background.js as it can reach other websites thus
* bypassing the same-origin policy.
* @param {string} url The URL to check to be WebP.
* @param {Object} obj the DOM object to modify if it is WebP.
*/
function TestUrl(url, obj) {
if (!URL_TO_IMG.hasOwnProperty(url)) URL_TO_IMG[url] = [];
URL_TO_IMG[url].push(obj);
PORT.postMessage({
"request": "webp_test_url",
"url": url
});
}
PORT.onMessage.addListener(function(msg) {
if (msg == undefined) return;
if (msg["request"] == "webp_test_url") {
var url = msg["url"];
// Change all DOM images with the same URL.
for(var i = 0; i < URL_TO_IMG[url].length; ++i) {
ChangeWebPImage(URL_TO_IMG[url][i], url, msg["quality"], msg["type"]);
}
}
});
/**
* Main function to test the DOM for any WebP. Called whenever the DOM is loaded
* or updated.
* Note: in jQuery, img.attr("src") returns what is in the HTML (which could be
* a relative path), img[0].src the absolute path, hence what we need.
*/
var TestForWebp = function() {
chrome.storage.local.get('is_enabled', function(data) {
if (!data['is_enabled']) {
return;
}
// Only check for WebP once every 2 seconds.
if ((new Date()).getTime() - TEST_TIME < 2000) {
return;
}
// Clear the matchings from URL's to DOM objects.
URL_TO_IMG = {};
// Good old images.
$('img,amp-img').each(function() {
var img = $(this);
if (img[0].src == undefined) {
return;
}
TestUrl(img[0].src, img);
});
// Deal with pictures (The Guardian).
$('picture').each(function() {
var img = $(this).children("img")[0];
$(this).children("source").each(function() {
src = $(this)[0].src;
TestUrl(src, img);
var srcset = $(this)[0].srcset;
if (srcset != undefined) {
var start = 0;
var end = 0;
var has_space = false;
// Follow the specs detailed at
// http://w3c.github.io/html/semantics-embedded-content.html
// to properly parse srcset (even if there is a comma in the name...).
srcset = srcset.trim();
for (var i = 0; i < srcset.length; ++i) {
// Check for any white space.
if (/\s/g.test(srcset[i])) {
has_space = true;
end = i;
}
if (srcset[i] == "," || i == srcset.length - 1) {
var src;
if (has_space) {
src = srcset.substring(start, end);
} else {
// If no space was found before, we need to have a space after
// the comma introducing the next string.
if (!/\s/g.test(srcset[i + 1])) continue;
src = srcset.substring(start, i);
}
TestUrl(src, img);
// Get to the next non-white space.
for (++i; i < srcset.length && /\s/g.test(srcset[i]); ++i);
start = i;
has_space = false;
}
}
}
});
});
// Some divs have a WebP background image (Netflix).
// Some links have a WebP background (Cultural Institute).
$('div,a').each(function() {
var div = $(this);
var url = div.css("background-image");
if (url == undefined || url == "none") {
return;
}
// Figure out the image URLs. Usually, the string is something like:
// 'url("url/image.webp")'
// But sometimes, there can be multiple, complex ones:
// 'url("url/image.webp"), url("url/filters:format(webp)/image.webp")'
// The URLs can include the following characters ',()'.
// We find the URLs with brute force (a proper regex is more complex).
for(var i = 0; i < url.length; ++i) {
i = url.indexOf("url(", i);
if (i < 0) break;
var count = 1;
// Find the closing parenthesis.
i += 4;
var i_ini = i;
for(; count != 0 && i < url.length; ++i) {
count += (url.charAt(i) == "(") - (url.charAt(i) == ")");
}
if (count != 0) break;
var src = url.slice(i_ini, i - 1);
if (src.charAt(0) == "'" || src.charAt(0) == '"') {
src = src.slice(1, -1);
}
TestUrl(src, div);
}
});
TEST_TIME = (new Date()).getTime();
});
};
// Analyze data after a scroll.
window.addEventListener("scroll", TestForWebp);
// React when the tab gets updated (as triggered in background.js).
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg == undefined || !msg.hasOwnProperty("request")) return;
if (msg["request"] == "webp_test_tab") {
TestForWebp();
// Some pages are still loading after onCompleted so also update
// after 1s and 10s.
setTimeout(TestForWebp, 1000);
setTimeout(TestForWebp, 10000);
} else if (msg["request"] == "webp_on_new_types") {
// Only update the header with all the types in the tab if we are in the
// main iframe.
if (window == window.top) {
UpdateIFrameHeader(new Set(msg["tab_types"]));
}
}
});
TestForWebp();