|
1 | 1 | function section_screenshot_evaluate({ section, elements_to_disable }) { |
2 | | - console.log(`Section: ${section}`); // skipcq |
3 | | - console.log(`Elements to disable: ${elements_to_disable}`); // skipcq |
| 2 | + console.log(`Section: ${section}`) // skipcq |
| 3 | + console.log(`Elements to disable: ${elements_to_disable}`) // skipcq |
4 | 4 |
|
5 | | - const levels = ["H1", "H2", "H3", "H4", "H5", "H6"]; |
6 | | - let sec = document.getElementById(section); |
7 | | - while (sec) { |
8 | | - if (levels.includes(sec.tagName)) { |
9 | | - break; |
| 5 | + const levels = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6'] |
| 6 | + const headingSelector = levels.join(',').toLowerCase() |
| 7 | + |
| 8 | + function findHeadingAfterAnchor(anchor) { |
| 9 | + let current = anchor |
| 10 | + while (current && current.parentElement) { |
| 11 | + if (current !== anchor && current.textContent.trim()) { |
| 12 | + return null |
| 13 | + } |
| 14 | + let sibling = current.nextElementSibling |
| 15 | + while (sibling) { |
| 16 | + if (levels.includes(sibling.tagName)) { |
| 17 | + return sibling |
| 18 | + } |
| 19 | + const nestedHeading = sibling.querySelector(headingSelector) |
| 20 | + if (nestedHeading) { |
| 21 | + return nestedHeading |
| 22 | + } |
| 23 | + if (sibling.textContent.trim() || sibling.children.length) { |
| 24 | + break |
| 25 | + } |
| 26 | + sibling = sibling.nextElementSibling |
| 27 | + } |
| 28 | + current = current.parentElement |
| 29 | + } |
| 30 | + return null |
| 31 | + } |
| 32 | + |
| 33 | + function findHeadingBeforeAnchor(anchor) { |
| 34 | + let previousHeading = null |
| 35 | + for (const heading of document.querySelectorAll(headingSelector)) { |
| 36 | + const position = heading.compareDocumentPosition(anchor) |
| 37 | + if (position & Node.DOCUMENT_POSITION_FOLLOWING) { |
| 38 | + previousHeading = heading |
| 39 | + } else if (position & Node.DOCUMENT_POSITION_PRECEDING) { |
| 40 | + break |
| 41 | + } |
10 | 42 | } |
11 | | - sec = sec.parentNode; |
| 43 | + return previousHeading |
| 44 | + } |
| 45 | + |
| 46 | + const target = document.getElementById(section) |
| 47 | + if (!target) { |
| 48 | + return |
12 | 49 | } |
13 | | - let sec_level = sec.tagName; |
14 | | - console.log(sec_level); |
| 50 | + |
| 51 | + let heading = target.closest(headingSelector) |
| 52 | + if (!heading && target.tagName === 'SPAN' && target.classList.contains('anchor')) { |
| 53 | + heading = findHeadingAfterAnchor(target) || findHeadingBeforeAnchor(target) |
| 54 | + } |
| 55 | + if (!heading) { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + let sec = heading |
| 60 | + let sec_level = heading.tagName |
| 61 | + console.log(sec_level) |
15 | 62 | if ( |
16 | | - sec.parentNode.className.includes("ext-discussiontools-init-section") || |
17 | | - sec.parentNode.className.includes("mw-heading") |
| 63 | + sec.parentElement.classList.contains('ext-discussiontools-init-section') || |
| 64 | + sec.parentElement.classList.contains('mw-heading') |
18 | 65 | ) { |
19 | 66 | // wo yi ding yao sha le ni men |
20 | | - sec = sec.parentNode; |
| 67 | + sec = sec.parentNode |
21 | 68 | } |
22 | 69 |
|
23 | | - let nbox = document.createElement("div"); |
24 | | - nbox.className = "bot-sectionbox"; |
25 | | - nbox.style = "display: inline-block"; |
26 | | - nbox.appendChild(sec.cloneNode(true)); |
| 70 | + let nbox = document.createElement('div') |
| 71 | + nbox.className = 'bot-sectionbox' |
| 72 | + nbox.style = 'display: inline-block' |
| 73 | + nbox.appendChild(sec.cloneNode(true)) |
27 | 74 |
|
28 | | - let next_sibling = sec.nextSibling; |
| 75 | + let next_sibling = sec.nextSibling |
29 | 76 | while (next_sibling) { |
30 | 77 | if (levels.includes(next_sibling.tagName)) { |
31 | | - if (levels.indexOf(next_sibling.tagName) <= levels.indexOf(sec_level)) |
32 | | - break; |
| 78 | + if (levels.indexOf(next_sibling.tagName) <= levels.indexOf(sec_level)) break |
33 | 79 | } |
34 | 80 | if ( |
35 | | - next_sibling.tagName === "DIV" && |
36 | | - (next_sibling.className.includes("ext-discussiontools-init-section") || |
37 | | - next_sibling.className.includes("mw-heading")) |
| 81 | + next_sibling.tagName === 'DIV' && |
| 82 | + (next_sibling.className.includes('ext-discussiontools-init-section') || |
| 83 | + next_sibling.className.includes('mw-heading')) |
38 | 84 | ) { |
39 | | - let child = next_sibling.firstChild; |
40 | | - let bf = false; |
| 85 | + let child = next_sibling.firstChild |
| 86 | + let bf = false |
41 | 87 | while (child) { |
42 | | - console.log(`Child tag: ${child.tagName}`); // skipcq |
| 88 | + console.log(`Child tag: ${child.tagName}`) // skipcq |
43 | 89 | if ( |
44 | 90 | levels.includes(child.tagName) && |
45 | 91 | levels.indexOf(child.tagName) <= levels.indexOf(sec_level) |
46 | 92 | ) { |
47 | | - bf = true; |
48 | | - break; |
| 93 | + bf = true |
| 94 | + break |
49 | 95 | } |
50 | | - child = child.nextSibling; |
| 96 | + child = child.nextSibling |
51 | 97 | } |
52 | | - if (bf) break; |
| 98 | + if (bf) break |
53 | 99 | } |
54 | | - nbox.appendChild(next_sibling.cloneNode(true)); |
55 | | - next_sibling = next_sibling.nextSibling; |
| 100 | + nbox.appendChild(next_sibling.cloneNode(true)) |
| 101 | + next_sibling = next_sibling.nextSibling |
56 | 102 | } |
57 | 103 |
|
58 | | - let lazyimg = nbox.querySelectorAll(".lazyload"); |
| 104 | + let lazyimg = nbox.querySelectorAll('.lazyload') |
59 | 105 | for (let i = 0; i < lazyimg.length; i++) { |
60 | | - lazyimg[i].className = "image"; |
61 | | - let dataSrc = lazyimg[i].getAttribute("data-src"); |
| 106 | + lazyimg[i].className = 'image' |
| 107 | + let dataSrc = lazyimg[i].getAttribute('data-src') |
62 | 108 | if ( |
63 | | - typeof dataSrc === "string" && |
64 | | - (dataSrc.startsWith("http://") || |
65 | | - dataSrc.startsWith("https://") || |
66 | | - dataSrc.startsWith("/")) |
| 109 | + typeof dataSrc === 'string' && |
| 110 | + (dataSrc.startsWith('http://') || dataSrc.startsWith('https://') || dataSrc.startsWith('/')) |
67 | 111 | ) { |
68 | | - lazyimg[i].src = dataSrc; |
| 112 | + lazyimg[i].src = dataSrc |
69 | 113 | } else { |
70 | | - console.warn(`Blocked suspicious data-src value for image: ${dataSrc}`); // skipcq |
| 114 | + console.warn(`Blocked suspicious data-src value for image: ${dataSrc}`) // skipcq |
71 | 115 | } |
72 | 116 | } |
73 | 117 |
|
74 | | - let new_parentNode = sec.parentNode.cloneNode(); |
75 | | - let pparentNode = sec.parentNode.parentNode; |
76 | | - pparentNode.removeChild(sec.parentNode); |
77 | | - pparentNode.appendChild(new_parentNode); |
78 | | - new_parentNode.appendChild(nbox); |
| 118 | + let new_parentNode = sec.parentNode.cloneNode() |
| 119 | + let pparentNode = sec.parentNode.parentNode |
| 120 | + pparentNode.removeChild(sec.parentNode) |
| 121 | + pparentNode.appendChild(new_parentNode) |
| 122 | + new_parentNode.appendChild(nbox) |
79 | 123 |
|
80 | 124 | for (let i = 0; i < elements_to_disable.length; i++) { |
81 | | - let element_to_boom = document.querySelector(elements_to_disable[i]); // :rina: :rina: :rina: :rina: |
| 125 | + let element_to_boom = document.querySelector(elements_to_disable[i]) // :rina: :rina: :rina: :rina: |
82 | 126 | if (element_to_boom != null) { |
83 | | - element_to_boom.style = "display: none !important"; |
| 127 | + element_to_boom.style = 'display: none !important' |
84 | 128 | } |
85 | 129 | } |
86 | 130 |
|
87 | | - document.querySelectorAll("*").forEach((element) => { |
88 | | - element.parentNode.replaceChild(element.cloneNode(true), element); |
89 | | - }); |
90 | | - window.scroll(0, 0); |
| 131 | + document.querySelectorAll('*').forEach((element) => { |
| 132 | + element.parentNode.replaceChild(element.cloneNode(true), element) |
| 133 | + }) |
| 134 | + window.scroll(0, 0) |
91 | 135 | } |
0 commit comments