From f441e19586dfe0875691be37d3caae2bfe02f788 Mon Sep 17 00:00:00 2001 From: iorate Date: Sun, 22 Sep 2019 10:46:30 +0900 Subject: [PATCH] Use 'AutoPagerize_DOMNodeInserted' to work with AutoPagerize --- src/ts/content.ts | 61 ++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/src/ts/content.ts b/src/ts/content.ts index 250991cc6..9734a6bd7 100644 --- a/src/ts/content.ts +++ b/src/ts/content.ts @@ -22,47 +22,37 @@ class Main { constructor() { (async () => { - this.onBlacklistsLoaded(await loadBlacklists()); + this.blacklists = await loadBlacklists(); + for (const entry of this.queuedEntries) { + this.judgeEntry(entry); + } + this.queuedEntries.length = 0; })(); new MutationObserver(records => { - this.onDOMContentMutated(records); + if (!$('ubShowStyle') && document.head) { + this.setupStyleSheets(); + } + for (const record of records) { + for (const node of record.addedNodes) { + if (node.nodeType === Node.ELEMENT_NODE) { + this.onElementAdded(node as HTMLElement); + } + } + } }).observe(document.documentElement, { childList: true, subtree: true }); + document.addEventListener('AutoPagerize_DOMNodeInserted', e => { + for (const element of (e.target as HTMLElement).querySelectorAll('.g')) { + this.onElementAdded(element); + } + }); document.addEventListener('DOMContentLoaded', () => { - this.onDOMContentLoaded(); + this.setupControl(); + this.setupBlacklistUpdateDialog(); }); } - onBlacklistsLoaded(blacklists: BlacklistAggregation): void { - this.blacklists = blacklists; - for (const entry of this.queuedEntries) { - this.judgeEntry(entry); - } - this.queuedEntries.length = 0; - } - - onDOMContentMutated(records: MutationRecord[]): void { - if (!$('ubShowStyle') && document.head) { - this.setupStyleSheets(); - } - for (const record of records) { - for (const node of record.addedNodes) { - if (node.nodeType === Node.ELEMENT_NODE) { - const element = node as HTMLElement; - this.onElementAdded(element); - - // Workaround for AutoPagerize - if (element.matches('p.autopagerize_page_info ~ div.bkWMgd')) { - for (const child of element.querySelectorAll('div.g')) { - this.onElementAdded(child); - } - } - } - } - } - } - onElementAdded(element: HTMLElement): void { const entryInfo = inspectEntry(element); if (entryInfo) { @@ -75,11 +65,6 @@ class Main { } } - onDOMContentLoaded(): void { - this.setupControl(); - this.setupBlockDialogs(); - } - setupStyleSheets(): void { const insertRules = (sheet: CSSStyleSheet, rulesString: string): void => { const rules = rulesString.match(/[^}]+\}/g)!; @@ -244,7 +229,7 @@ class Main { this.updateControl(); } - setupBlockDialogs(): void { + setupBlacklistUpdateDialog(): void { const blacklistUpdateDialog = document.createElement('dialog'); // #if BROWSER === 'firefox' dialogPolyfill.registerDialog(blacklistUpdateDialog);