Skip to content

Commit 13153d3

Browse files
authored
refactor: optimize event listener for vue:settled to handle HTMLTableElement variants (#234)
* refactor: optimize event listener for `vue:settled` to handle HTMLTableElement variants * fix: refine filtering logic for HTML elements based on size constraints
1 parent f573f57 commit 13153d3

1 file changed

Lines changed: 39 additions & 17 deletions

File tree

userscript/source/index.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,16 @@ 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)
7161
.filter(Child =>
7262
Child instanceof HTMLImageElement ||
7363
getComputedStyle(Child).backgroundImage !== 'none'
74-
)
64+
).filter(Child => parseFloat(getComputedStyle(Child).getPropertyValue('width')) >= 5 && parseFloat(getComputedStyle(Child).getPropertyValue('height')) >= 5)
65+
.filter(Child => parseFloat(getComputedStyle(Child).getPropertyValue('width')) <= 50 && parseFloat(getComputedStyle(Child).getPropertyValue('height')) <= 50)
7566
let MatchedCount = 0
7667
for (const Child of CandidateChildren) {
7768
const Result = await OCRInstance.DetectFromElement(Child, {
@@ -87,17 +78,48 @@ export async function RunNamuLinkUserscript(BrowserWindow: typeof window, Usersc
8778
}
8879
}
8980
return NextTargeted
90-
})()
81+
}
82+
83+
ArticleHTMLElement.addEventListener('vue:settled', async () => {
84+
let Targeted = [...document.querySelectorAll('#app div[class] div[class] ~ div[class]')].filter(Ele => Ele instanceof HTMLElement)
85+
Targeted = Targeted.filter(Ele =>
86+
parseFloat(getComputedStyle(Ele).getPropertyValue('padding-top')) >= 20 ||
87+
parseFloat(getComputedStyle(Ele).getPropertyValue('margin-top')) >= 20 ||
88+
parseFloat(getComputedStyle(Ele).getPropertyValue('margin-bottom')) >= 12.5
89+
)
90+
Targeted = Targeted.filter(Ele => {
91+
let Children = [...Ele.querySelectorAll('*')].filter(Child => Child instanceof HTMLElement)
92+
// non-HTMLTableElement
93+
if (Children.filter(Child =>
94+
parseFloat(getComputedStyle(Child).getPropertyValue('padding-top')) >= 5 &&
95+
parseFloat(getComputedStyle(Child).getPropertyValue('border-bottom-width')) >= 0.1
96+
).length === 1) return true
97+
// HTMLTableElement
98+
return Children.filter(Child => (Child instanceof HTMLTableElement || Child instanceof HTMLTableCellElement) &&
99+
parseFloat(getComputedStyle(Child).getPropertyValue('padding-top')) >= 5 && parseFloat(getComputedStyle(Child).getPropertyValue('padding-bottom')) >= 5).length >= 2
100+
})
101+
Targeted = Targeted.filter(Ele => {
102+
let Children = [...Ele.querySelectorAll('*')].filter(Child => Child instanceof HTMLElement)
103+
return !Children.some(Child => {
104+
return parseFloat(getComputedStyle(Child).getPropertyValue('margin-bottom')) >= 10 && parseFloat(getComputedStyle(Child).getPropertyValue('padding-bottom')) >= 1 && parseFloat(getComputedStyle(Child).getPropertyValue('padding-top')) >= 1 &&
105+
parseFloat(getComputedStyle(Child).getPropertyValue('border-top-width')) >= 0.25 && parseFloat(getComputedStyle(Child).getPropertyValue('border-bottom-width')) >= 0.25
106+
})
107+
})
108+
Targeted = await ExecuteOCR(Targeted)
91109
Targeted.forEach(Ele => Targeted.push(...new Set([...Ele.querySelectorAll('*')].filter(Child => Child instanceof HTMLElement))))
92110
Targeted = [...new Set(Targeted)]
93111
let RealTargeted = Targeted.filter(Ele => parseFloat(getComputedStyle(Ele).getPropertyValue('padding-left')) >= 5 && parseFloat(getComputedStyle(Ele).getPropertyValue('border-right-width')) >= 0.1)
94112
console.debug(`[${UserscriptName}] vue:settled RealTargeted`, RealTargeted)
95113
RealTargeted.forEach(Ele => {
96114
Ele.style.setProperty('display', 'none', 'important')
97115
})
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 => {
116+
let RealTabletTargeted = Targeted.filter(Ele => {
117+
if (!(Ele instanceof HTMLElement) || !(Ele instanceof HTMLTableElement)) return false
118+
let Children = [...Ele.querySelectorAll('*')].filter(Child => Child instanceof HTMLElement)
119+
return Children.some(Child => parseFloat(getComputedStyle(Child).getPropertyValue('padding-top')) >= 5 && parseFloat(getComputedStyle(Child).getPropertyValue('padding-bottom')) >= 5)
120+
})
121+
console.debug(`[${UserscriptName}] vue:settled RealTabletTargeted`, RealTabletTargeted)
122+
RealTabletTargeted.forEach(Ele => {
101123
Ele.style.setProperty('display', 'none', 'important')
102124
})
103125
})

0 commit comments

Comments
 (0)