-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
272 lines (228 loc) · 7.55 KB
/
main.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
const workerPromise = new Promise(async (resolve) => {
const response = await fetch(chrome.runtime.getURL("worker.js"));
const blob = await response.blob();
const worker = new Worker(URL.createObjectURL(blob));
listenOnWorker(worker);
resolve(worker);
});
const imageMap = new Map();
const idMap = new Map();
if (
document.readyState === "interactive" ||
document.readyState === "complete"
) {
update();
pixelatedNewImages();
} else {
document.addEventListener("DOMContentLoaded", update);
window.addEventListener("load", update);
document.addEventListener("DOMContentLoaded", pixelatedNewImages);
window.addEventListener("load", pixelatedNewImages);
}
window.addEventListener("scroll", debounce(pixelatedNewImages, 1000), {
passive: true,
});
function update() {
updateFavicon();
updateLogo();
updateSprites();
updateTab2233();
updatePacman();
updateHeaderBanner();
}
function updateFavicon() {
const link =
document.querySelector("link[rel*='icon']") ||
document.createElement("link");
link.type = "image/png";
link.rel = "icon";
link.href = chrome.runtime.getURL("images/pilipili.png");
document.querySelector("head").appendChild(link);
}
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
let timeout;
return function () {
const context = this,
args = arguments;
const later = function () {
timeout = null;
if (!immediate) func.apply(context, args);
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
function updateHeaderBanner() {
const banner = document.querySelector(".international-header .bili-banner");
const bannerLink = document.querySelector(
".international-header .banner-link"
);
if (banner && bannerLink) {
bannerLink.style.backgroundImage = banner.style.backgroundImage;
banner.style.backgroundImage = "none";
}
}
function updateLogo() {
const logo = document.querySelector(
".bili-banner .b-logo .head-logo .logo-img"
);
if (logo) {
logo.src = chrome.runtime.getURL("images/logo.svg");
}
}
// Replace the SVG sprite
async function updateSprites() {
const target = document.querySelector("body > svg:first-child");
if (target) {
const spritesUrl = chrome.runtime.getURL("images/sprite.symbol.svg");
const response = await fetch(spritesUrl);
const svgText = await response.text();
const svgElement = new DOMParser().parseFromString(svgText, "image/svg+xml")
.documentElement;
svgElement.setAttribute("aria-hidden", "true");
svgElement.style.cssText =
"position: absolute; width: 0px; height: 0px; overflow: hidden;";
target.parentElement.replaceChild(svgElement, target);
}
}
function updateTab2233() {
const target = document.querySelector(".elevator .bg23");
if (target) {
target.style.backgroundImage = `url(${chrome.runtime.getURL(
"images/tab2233.png"
)})`;
}
}
function updatePacman() {
const targets = document.querySelector(".home-slide .trigger");
if (targets) {
targets.style = `--pacman: url("${chrome.runtime.getURL(
"images/pacman.png"
)}")`;
}
}
////////////////////////////////////////////////////////////////////////////////
// import "regenerator-runtime/runtime";
function pixelatedNewImages() {
document
// Exclude the site logo
.querySelectorAll(
".international-home .first-screen img:not([data-pixelated]), .international-home .storey-box img:not([data-pixelated])"
)
.forEach((image) => {
image.crossOrigin = "anonymous";
image.dataset.pixelated = "ongoing";
if (image.complete && image.naturalWidth) {
pixelateImage(image);
} else {
image.addEventListener("load", (e) => {
pixelateImage(e.target);
});
}
});
}
function pixelateImage(image) {
// If you just want to pixelate human bodies in the image,
// use pixelateBodiesInImages(image) instead of pixelateTheWholeImage(image)
pixelateTheWholeImage(image);
}
// Send to work for masking
async function pixelateBodiesInImages(image) {
const id = generateId();
if (idMap.has(image)) {
return;
} else {
imageMap.set(id, image);
idMap.set(image, id);
}
const canvas = document.createElement("canvas");
canvas.width = image.naturalWidth;
canvas.height = image.naturalHeight;
const ctx = canvas.getContext("2d");
ctx.drawImage(image, 0, 0);
const imageData = await ctx.getImageData(0, 0, canvas.width, canvas.height);
const pixels = imageData.data.buffer;
const worker = await workerPromise;
worker.postMessage(
{
id,
pixels,
width: imageData.width,
height: imageData.height,
},
[imageData.data.buffer]
);
}
function listenOnWorker(worker) {
// Get the mask from worker and draw it
worker.addEventListener("message", async ({ data }) => {
const { id, maskBitmap } = data;
const image = imageMap.get(id);
const blob = await pixelateByMask(image, maskBitmap);
console.log("done:", image);
const blobUrl = URL.createObjectURL(blob);
image.src = blobUrl;
image.addEventListener("load", (e) => {
e.target.dataset.pixelated = "done";
URL.revokeObjectURL(blobUrl);
});
});
}
async function pixelateByMask(image, maskBitmap, factor = 60) {
const canvas = document.createElement("canvas");
canvas.width = image.naturalWidth;
canvas.height = image.naturalHeight;
const ctx = canvas.getContext("2d");
ctx.globalCompositeOperation = "copy";
ctx.drawImage(maskBitmap, 0, 0);
// draw the original image first
ctx.globalCompositeOperation = "source-in";
ctx.drawImage(image, 0, 0);
/// calculate the factor
const fw = Math.round((canvas.width * factor) / 1000);
const fh = Math.round((canvas.height * factor) / 1000);
/// draw mini-version of image
ctx.imageSmoothingEnabled = false;
ctx.globalCompositeOperation = "copy";
ctx.drawImage(canvas, 0, 0, fw, fh);
/// draw the mini-version back up, voila, pixelated
ctx.drawImage(canvas, 0, 0, fw, fh, 0, 0, canvas.width, canvas.height);
// overlay the mosaic area over the original image
ctx.imageSmoothingEnabled = true;
ctx.globalCompositeOperation = "destination-over";
ctx.drawImage(image, 0, 0);
return new Promise((resolve) => canvas.toBlob((blob) => resolve(blob)));
}
function generateId() {
return "_" + Math.random().toString(36).substr(2, 9);
}
async function pixelateTheWholeImage(image, factor = 60) {
const canvas = document.createElement("canvas");
canvas.width = image.naturalWidth;
canvas.height = image.naturalHeight;
const ctx = canvas.getContext("2d");
// draw the original image first
ctx.drawImage(image, 0, 0);
/// calculate the factor
const fw = Math.round((canvas.width * factor) / 1000);
const fh = Math.round((canvas.height * factor) / 1000);
/// draw mini-version of image
ctx.imageSmoothingEnabled = false;
ctx.drawImage(canvas, 0, 0, fw, fh);
/// draw the mini-version back up, voila, pixelated
ctx.drawImage(canvas, 0, 0, fw, fh, 0, 0, canvas.width, canvas.height);
const blob = await new Promise((resolve) =>
canvas.toBlob((blob) => resolve(blob))
);
image.src = URL.createObjectURL(blob);
image.addEventListener("load", (e) => {
e.target.dataset.pixelated = "done";
URL.revokeObjectURL(URL.createObjectURL(blob));
});
}