Skip to content

Commit 60850ef

Browse files
committed
refactor: optimize event listener for vue:settled to handle HTMLTableElement variants
1 parent f573f57 commit 60850ef

1 file changed

Lines changed: 37 additions & 16 deletions

File tree

userscript/source/index.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,8 @@ export async function RunNamuLinkUserscript(BrowserWindow: typeof window, Usersc
5353

5454
const OCRInstance = CreateOcrWorkerClient(BrowserWindow, new Worker(URL.createObjectURL(new Blob([__OCR_WORKER_CODE__], { type: 'application/javascript' }))))
5555

56-
ArticleHTMLElement.addEventListener('vue:settled', async () => {
57-
let Targeted = [...document.querySelectorAll('#app div[class] div[class] ~ div[class]')].filter(Ele => Ele instanceof HTMLElement)
58-
Targeted = Targeted.filter(Ele =>
59-
parseFloat(getComputedStyle(Ele).getPropertyValue('padding-top')) >= 20 ||
60-
parseFloat(getComputedStyle(Ele).getPropertyValue('margin-top')) >= 20 ||
61-
parseFloat(getComputedStyle(Ele).getPropertyValue('margin-bottom')) >= 12.5
62-
)
63-
Targeted = Targeted.filter(Ele => [...Ele.querySelectorAll('*')].filter(Child =>
64-
parseFloat(getComputedStyle(Child).getPropertyValue('padding-top')) >= 5 && parseFloat(getComputedStyle(Child).getPropertyValue('border-bottom-width')) >= 0.1
65-
).length === 1)
66-
Targeted = await (async () => {
67-
const NextTargeted = []
56+
async function ExecuteOCR(Targeted: HTMLElement[]) {
57+
const NextTargeted = []
6858
for (const Parent of Targeted) {
6959
const CandidateChildren = [...Parent.querySelectorAll('*')]
7060
.filter(Child => Child instanceof HTMLElement)
@@ -87,17 +77,48 @@ export async function RunNamuLinkUserscript(BrowserWindow: typeof window, Usersc
8777
}
8878
}
8979
return NextTargeted
90-
})()
80+
}
81+
82+
ArticleHTMLElement.addEventListener('vue:settled', async () => {
83+
let Targeted = [...document.querySelectorAll('#app div[class] div[class] ~ div[class]')].filter(Ele => Ele instanceof HTMLElement)
84+
Targeted = Targeted.filter(Ele =>
85+
parseFloat(getComputedStyle(Ele).getPropertyValue('padding-top')) >= 20 ||
86+
parseFloat(getComputedStyle(Ele).getPropertyValue('margin-top')) >= 20 ||
87+
parseFloat(getComputedStyle(Ele).getPropertyValue('margin-bottom')) >= 12.5
88+
)
89+
Targeted = Targeted.filter(Ele => {
90+
let Children = [...Ele.querySelectorAll('*')].filter(Child => Child instanceof HTMLElement)
91+
// non-HTMLTableElement
92+
if (Children.filter(Child =>
93+
parseFloat(getComputedStyle(Child).getPropertyValue('padding-top')) >= 5 &&
94+
parseFloat(getComputedStyle(Child).getPropertyValue('border-bottom-width')) >= 0.1
95+
).length === 1) return true
96+
// HTMLTableElement
97+
return Children.filter(Child => (Child instanceof HTMLTableElement || Child instanceof HTMLTableCellElement) &&
98+
parseFloat(getComputedStyle(Child).getPropertyValue('padding-top')) >= 5 && parseFloat(getComputedStyle(Child).getPropertyValue('padding-bottom')) >= 5).length >= 2
99+
})
100+
Targeted = Targeted.filter(Ele => {
101+
let Children = [...Ele.querySelectorAll('*')].filter(Child => Child instanceof HTMLElement)
102+
return !Children.some(Child => {
103+
return parseFloat(getComputedStyle(Child).getPropertyValue('margin-bottom')) >= 10 && parseFloat(getComputedStyle(Child).getPropertyValue('padding-bottom')) >= 1 && parseFloat(getComputedStyle(Child).getPropertyValue('padding-top')) >= 1 &&
104+
parseFloat(getComputedStyle(Child).getPropertyValue('border-top-width')) >= 0.25 && parseFloat(getComputedStyle(Child).getPropertyValue('border-bottom-width')) >= 0.25
105+
})
106+
})
107+
Targeted = await ExecuteOCR(Targeted)
91108
Targeted.forEach(Ele => Targeted.push(...new Set([...Ele.querySelectorAll('*')].filter(Child => Child instanceof HTMLElement))))
92109
Targeted = [...new Set(Targeted)]
93110
let RealTargeted = Targeted.filter(Ele => parseFloat(getComputedStyle(Ele).getPropertyValue('padding-left')) >= 5 && parseFloat(getComputedStyle(Ele).getPropertyValue('border-right-width')) >= 0.1)
94111
console.debug(`[${UserscriptName}] vue:settled RealTargeted`, RealTargeted)
95112
RealTargeted.forEach(Ele => {
96113
Ele.style.setProperty('display', 'none', 'important')
97114
})
98-
let FrameTargeted = Targeted.filter(Ele => Ele instanceof HTMLElement && Ele.innerText.trim().length === 0)
99-
console.debug(`[${UserscriptName}] vue:settled FrameTargeted`, FrameTargeted)
100-
FrameTargeted.forEach(Ele => {
115+
let RealTabletTargeted = Targeted.filter(Ele => {
116+
if (!(Ele instanceof HTMLElement) || !(Ele instanceof HTMLTableElement)) return false
117+
let Children = [...Ele.querySelectorAll('*')].filter(Child => Child instanceof HTMLElement)
118+
return Children.some(Child => parseFloat(getComputedStyle(Child).getPropertyValue('padding-top')) >= 5 && parseFloat(getComputedStyle(Child).getPropertyValue('padding-bottom')) >= 5)
119+
})
120+
console.debug(`[${UserscriptName}] vue:settled RealTabletTargeted`, RealTabletTargeted)
121+
RealTabletTargeted.forEach(Ele => {
101122
Ele.style.setProperty('display', 'none', 'important')
102123
})
103124
})

0 commit comments

Comments
 (0)