|
|
@@ -35,9 +35,20 @@ this.singlefile.lib.processors.lazy.content.loader = this.singlefile.lib.process
|
|
|
const removeEventListener = (type, listener, options) => window.removeEventListener(type, listener, options);
|
|
|
const timeouts = new Map();
|
|
|
|
|
|
+ browser.runtime.onMessage.addListener(message => {
|
|
|
+ if (message.method == "singlefile.lazyTimeout.onTimeout") {
|
|
|
+ const timeoutData = timeouts.get(message.type);
|
|
|
+ if (timeoutData) {
|
|
|
+ timeouts.delete(message.type);
|
|
|
+ timeoutData.callback();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
return {
|
|
|
process: async options => {
|
|
|
if (document.documentElement) {
|
|
|
+ timeouts.clear();
|
|
|
const maxScrollY = Math.max(document.documentElement.scrollHeight - (document.documentElement.clientHeight * 1.5), 0);
|
|
|
const maxScrollX = Math.max(document.documentElement.scrollWidth - (document.documentElement.clientWidth * 1.5), 0);
|
|
|
if (window.scrollY <= maxScrollY && window.scrollX <= maxScrollX) {
|
|
|
@@ -154,18 +165,11 @@ this.singlefile.lib.processors.lazy.content.loader = this.singlefile.lib.process
|
|
|
|
|
|
async function setAsyncTimeout(type, callback, delay) {
|
|
|
if (browser && browser.runtime && browser.runtime.sendMessage) {
|
|
|
- if (!timeouts.get(type)) {
|
|
|
- timeouts.set(type, callback);
|
|
|
+ if (!timeouts.get(type) || !timeouts.get(type).pending) {
|
|
|
+ const timeoutData = { callback, pending: true };
|
|
|
+ timeouts.set(type, timeoutData);
|
|
|
await browser.runtime.sendMessage({ method: "singlefile.lazyTimeout.setTimeout", type, delay });
|
|
|
- const timeoutCallback = message => {
|
|
|
- if (message.method == "singlefile.lazyTimeout.onTimeout" && message.type == type) {
|
|
|
- browser.runtime.onMessage.removeListener(timeoutCallback);
|
|
|
- callback();
|
|
|
- return Promise.resolve({});
|
|
|
- }
|
|
|
- };
|
|
|
- timeouts.delete(type, callback);
|
|
|
- browser.runtime.onMessage.addListener(timeoutCallback);
|
|
|
+ timeoutData.pending = false;
|
|
|
}
|
|
|
} else {
|
|
|
const timeoutId = timeouts.get(type);
|