Skip to content

Commit

Permalink
Use 'AutoPagerize_DOMNodeInserted' to work with AutoPagerize
Browse files Browse the repository at this point in the history
  • Loading branch information
iorate committed Sep 22, 2019
1 parent e3f3bab commit f441e19
Showing 1 changed file with 23 additions and 38 deletions.
61 changes: 23 additions & 38 deletions src/ts/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLElement>('.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<HTMLElement>('div.g')) {
this.onElementAdded(child);
}
}
}
}
}
}

onElementAdded(element: HTMLElement): void {
const entryInfo = inspectEntry(element);
if (entryInfo) {
Expand All @@ -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)!;
Expand Down Expand Up @@ -244,7 +229,7 @@ class Main {
this.updateControl();
}

setupBlockDialogs(): void {
setupBlacklistUpdateDialog(): void {
const blacklistUpdateDialog = document.createElement('dialog');
// #if BROWSER === 'firefox'
dialogPolyfill.registerDialog(blacklistUpdateDialog);
Expand Down

0 comments on commit f441e19

Please sign in to comment.