Skip to content

Commit 9e334ee

Browse files
committed
1.2.45 really preload next in gallery for url array
1 parent 02906f5 commit 9e334ee

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

.eslintrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ rules:
3232
arrow-parens: [2, as-needed]
3333
arrow-spacing: [2, {before: true, after: true}]
3434
block-scoped-var: [2]
35-
brace-style: [2]
35+
brace-style: [2, 1tbs, {allowSingleLine: true}]
3636
camelcase: [2, {properties: never, allow: ["^GM_\\w+"]}]
3737
class-methods-use-this: [2]
3838
comma-dangle: [2, {arrays: always-multiline, objects: always-multiline, functions: ignore}]

script.user.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// @grant GM.setValue
2626
// @grant GM.xmlHttpRequest
2727
//
28-
// @version 1.2.44
28+
// @version 1.2.45
2929
// @author tophf
3030
//
3131
// @original-version 2017.9.29
@@ -103,7 +103,7 @@ let trustedHTML, trustedScript;
103103
//#region GM4 polyfill
104104

105105
if (typeof GM === 'undefined' || !GM.xmlHttpRequest)
106-
this.GM = {info: GM_info};
106+
this.GM = {__proto__: null, info: GM_info};
107107
if (!GM.getValue)
108108
GM.getValue = GM_getValue; // we use it only with `await` so no need to return a Promise
109109
if (!GM.setValue)
@@ -1163,15 +1163,15 @@ const Gallery = {
11631163

11641164
next(dir) {
11651165
if (dir) ai.gIndex = Gallery.nextIndex(dir);
1166-
const item = ai.gItems[ai.gIndex];
1166+
const item = ai.gItem = ai.gItems[ai.gIndex];
11671167
if (Array.isArray(item.url)) {
11681168
ai.urls = item.url.slice(1);
11691169
ai.url = item.url[0];
11701170
} else {
11711171
ai.urls = null;
11721172
ai.url = item.url;
11731173
}
1174-
ai.preloadUrl = ensureArray(ai.gItems[Gallery.nextIndex(dir || 1)].url)[0];
1174+
ai.gItemNext = ai.gItems[Gallery.nextIndex(dir || 1)];
11751175
App.startSingle();
11761176
Bar.updateName();
11771177
if (dir) Bar.show(0);
@@ -1375,16 +1375,36 @@ const Popup = {
13751375
this.setAttribute('loaded', '');
13761376
ai.popupLoaded = true;
13771377
Status.set('-loading');
1378-
if (ai.preloadUrl) {
1379-
$new('img', {src: ai.preloadUrl});
1380-
ai.preloadUrl = null;
1381-
}
1378+
let i = ai.gItem;
1379+
if (i) i.url = this.src;
1380+
if ((i = ai.gItemNext))
1381+
Popup.preload(this, i);
13821382
}
13831383
},
13841384

13851385
onZoom() {
13861386
this.classList.remove(`${PREFIX}zooming`);
13871387
},
1388+
1389+
async preload(p, item, u = item.url, el = $new('img')) {
1390+
ai.gItemNext = null;
1391+
if (!Array.isArray(u)) {
1392+
el.src = u;
1393+
return;
1394+
}
1395+
for (const arr = u; p === ai.popup && item.url === arr && (u = arr[0]);) {
1396+
const {type} = await new Promise(cb => {
1397+
el.src = u;
1398+
el.onload = el.onerror = cb;
1399+
});
1400+
if (arr[0] === u)
1401+
arr.shift();
1402+
if (type === 'load') {
1403+
item.url = u;
1404+
break;
1405+
}
1406+
}
1407+
},
13881408
};
13891409

13901410
const PopupVideo = {

types/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ declare namespace mpiv {
143143
flipY: boolean
144144
force: boolean
145145
gIndex: number
146+
gItem: GalleryItem
147+
gItemNext: GalleryItem
146148
gItems: GalleryItemsArray
147149
gNum: number
148150
imageUrl: StringOrArrayOfStrings
@@ -158,8 +160,6 @@ declare namespace mpiv {
158160
popupLoaded: boolean
159161
/** time, ms */
160162
preloadStart: number
161-
/** used by gallery to preload the next image */
162-
preloadUrl: string
163163
rect: DOMRect
164164
/** is mouse still over PopupInfo.rect */
165165
rectHovered: boolean

0 commit comments

Comments
 (0)