-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dbd2a08
commit 8a2fc4b
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Finds a MediaWiki section heading from the current DOM using its title. | ||
* | ||
* @param sectionHeadingName The name of the section to find. | ||
* @param n The `n` of the section. Starts at 1. | ||
* @return The found section heading. `null` if not found. | ||
*/ | ||
export default function findSectionHeading( | ||
sectionHeadingName: string, | ||
n = 1 | ||
): HTMLElement | null { | ||
let currentN = 1; | ||
|
||
const headlines = Array.from( document.querySelectorAll( 'h2 > .mw-headline' ) ); | ||
for ( const el of headlines ) { | ||
if ( el instanceof HTMLElement && el.innerText === sectionHeadingName ) { | ||
if ( currentN >= n ) { | ||
return el.parentElement; | ||
} else { | ||
currentN++; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters