From 2f9863b9423eded644ab3c2649f1b06af5dfa784 Mon Sep 17 00:00:00 2001 From: NovemLinguae Date: Sat, 30 Mar 2024 10:34:10 -0700 Subject: [PATCH] fix linter errors --- .eslintrc.json | 3 +- CiteHighlighter/CiteHighlighter.js | 184 +++++++++++++++-------------- 2 files changed, 97 insertions(+), 90 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 7d83c22..9763ca9 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -18,6 +18,7 @@ "no-jquery/variable-pattern": 0, "no-shadow": 0, "no-jquery/no-global-selector": 0, - "camelcase": 0 + "camelcase": 0, + "mediawiki/class-doc": 0 } } diff --git a/CiteHighlighter/CiteHighlighter.js b/CiteHighlighter/CiteHighlighter.js index 878640b..67b378a 100644 --- a/CiteHighlighter/CiteHighlighter.js +++ b/CiteHighlighter/CiteHighlighter.js @@ -5,7 +5,7 @@ class CiteHighlighter { this.sources = await this.getListOfSourcesAndRatings(); this.unreliableWordsForOrangeHighlighting = this.getUnreliableWords(); this.setConfigVariableDefaultsIfNeeded(); - this.articleTitle = mw.config.get('wgPageName'); + this.articleTitle = mw.config.get( 'wgPageName' ); if ( this.isSlowPage() ) { return; } @@ -14,7 +14,7 @@ class CiteHighlighter { this.preventWikipediaFalsePositives(); this.colors = this.getColors(); this.writeCSS(); - this.wikicode = await this.getWikicode(this.articleTitle); + this.wikicode = await this.getWikicode( this.articleTitle ); this.addHTMLClassesToRefs(); this.addHTMLClassesForUnreliableWords(); this.observeAndAddClassesToTooltips(); @@ -57,13 +57,13 @@ class CiteHighlighter { 'weebly', 'wix', 'wordpress', - '/wp-', // WordPress, e.g. wp-content + '/wp-' // WordPress, e.g. wp-content ]; } async getListOfSourcesAndRatings() { - let sources = await this.getWikicodeFromCache('User:Novem Linguae/Scripts/CiteHighlighter/SourcesJSON.js'); - sources = JSON.parse(sources); + let sources = await this.getWikicodeFromCache( 'User:Novem Linguae/Scripts/CiteHighlighter/SourcesJSON.js' ); + sources = JSON.parse( sources ); return sources; } @@ -84,10 +84,12 @@ class CiteHighlighter { * * On pages with a lot of links (watchlist, WP:FA), highlighting EVERYTHING will double the * load time. e.g. watchlist 5 seconds -> 10 seconds. + * + * @return {boolean} isSlowPage */ isSlowPage() { if ( - mw.config.get('wgAction') === 'history' || + mw.config.get( 'wgAction' ) === 'history' || this.articleTitle === 'Main_Page' || this.articleTitle === 'Wikipedia:Featured_articles' || this.articleTitle === 'Special:Watchlist' || @@ -104,18 +106,18 @@ class CiteHighlighter { * false. Goal: easily see if the script is highlighting anything wrong. */ highlightSourceListsMoreAggressively() { - let highlightEverythingList = [ + const highlightEverythingList = [ 'Wikipedia:Reliable_sources/Perennial_sources', // 'Wikipedia:New_page_patrol_source_guide', // so slow that I hard-coded this never to load by placing a check in isSlowPage() 'Wikipedia:WikiProject_Albums/Sources', 'Wikipedia:WikiProject_Video_games/Sources#Reliable_sources', 'Wikipedia:WikiProject_Anime_and_manga/Online_reliable_sources', 'Wikipedia:WikiProject_Africa/Africa_Sources_List', - 'Wikipedia:WikiProject_Dungeons_%26_Dragons/References', + 'Wikipedia:WikiProject_Dungeons_%26_Dragons/References' ]; - if ( window.citeHighlighterAlwaysHighlightSourceLists == true) { - if ( highlightEverythingList.includes(this.articleTitle) ) { + if ( window.citeHighlighterAlwaysHighlightSourceLists ) { + if ( highlightEverythingList.includes( this.articleTitle ) ) { window.citeHighlighterHighlightEverything = true; } } @@ -126,7 +128,7 @@ class CiteHighlighter { * inline citations are malformed */ highlightDraftsMoreAggressively() { - if ( mw.config.get('wgNamespaceNumber') == 118 ) { + if ( mw.config.get( 'wgNamespaceNumber' ) === 118 ) { window.citeHighlighterHighlightEverything = true; } } @@ -136,21 +138,21 @@ class CiteHighlighter { */ preventWikipediaFalsePositives() { if ( window.citeHighlighterHighlightEverything ) { - this.deleteAll(this.sources, 'en.wikipedia.org', 'wikipedia.org', 'wiktionary.org'); - this.deleteFromArray(this.unreliableWordsForOrangeHighlighting, 'wiki'); + this.deleteAll( this.sources, 'en.wikipedia.org', 'wikipedia.org', 'wiktionary.org' ); + this.deleteFromArray( this.unreliableWordsForOrangeHighlighting, 'wiki' ); } } getColors() { if ( window.citeHighlighterLighterColors ) { return { - 'unreliableWord': '#ffb347', - 'preprint': '#ffcfd5', - 'doi': 'transparent', - 'medrs': '#63ff70', - 'green': '#a6ffb9', - 'yellow': '#ffffcc', - 'red': '#ffcfd5', + unreliableWord: '#ffb347', + preprint: '#ffcfd5', + doi: 'transparent', + medrs: '#63ff70', + green: '#a6ffb9', + yellow: '#ffffcc', + red: '#ffcfd5' }; } else { return { @@ -158,45 +160,47 @@ class CiteHighlighter { // order of these first 3 fixes an issue where published academic papers were being colored preprint red // lowest priority - 'unreliableWord': '#ffb347', // orange for now, for easier testing. later will be red. - 'preprint': 'lightcoral', - 'doi': 'transparent', - 'medrs': 'limegreen', - 'green': 'lightgreen', - 'yellow': 'khaki', - 'red': 'lightcoral', - // 'aggregator': 'plum', // turning off aggregator for now, red/yellow/green is nice and simple, purple makes the color scheme more complicated + unreliableWord: '#ffb347', // orange for now, for easier testing. later will be red. + preprint: 'lightcoral', + doi: 'transparent', + medrs: 'limegreen', + green: 'lightgreen', + yellow: 'khaki', + red: 'lightcoral' + // 'aggregator': 'plum', // turning off aggregator for now, red/yellow/green is nice and simple, purple makes the color scheme more complicated // highest priority }; } } writeCSS() { - for ( let key in this.colors ) { - mw.util.addCSS('.cite-highlighter-' + key + ' {background-color: ' + this.colors[key] + ';}'); - mw.util.addCSS('.rt-tooltipTail.cite-highlighter-' + key + '::after {background: ' + this.colors[key] + ';}'); + for ( const key in this.colors ) { + mw.util.addCSS( '.cite-highlighter-' + key + ' {background-color: ' + this.colors[ key ] + ';}' ); + mw.util.addCSS( '.rt-tooltipTail.cite-highlighter-' + key + '::after {background: ' + this.colors[ key ] + ';}' ); } } addHTMLClassesToRefs() { - for ( let color in this.colors ) { - let colorIsMissing = typeof this.sources[color] === 'undefined'; - if ( colorIsMissing ) continue; + for ( const color in this.colors ) { + const colorIsMissing = typeof this.sources[ color ] === 'undefined'; + if ( colorIsMissing ) { + continue; + } - for ( let source of this.sources[color] ) { + for ( const source of this.sources[ color ] ) { // Don't check the DOM for every domain. Too expensive. Instead, examine the wikitext and only check the DOM for domains found in the wikitext. // alwaysIncludeDomains are domains that we should always write a CSS rule for, even if they are not found in the wikitext. This makes sure that domains in {{Cite}} templates are detected. For example, {{Cite journal}} uses nih.gov, and {{Cite tweet}} uses twitter.com - let isAlwaysIncludeDomain = source === 'nih.gov' || source === 'twitter.com'; + const isAlwaysIncludeDomain = source === 'nih.gov' || source === 'twitter.com'; - if ( this.wikicode.includes(source) || isAlwaysIncludeDomain ) { - let isExternalLinkContainingDomainName = source.includes('.') && ! source.includes(' '); + if ( this.wikicode.includes( source ) || isAlwaysIncludeDomain ) { + const isExternalLinkContainingDomainName = source.includes( '.' ) && !source.includes( ' ' ); if ( isExternalLinkContainingDomainName ) { - this.highlightCitation(source, color); - this.highlightUnorderedListItem(source, color); + this.highlightCitation( source, color ); + this.highlightUnorderedListItem( source, color ); if ( window.citeHighlighterHighlightEverything ) { - this.highlightExternalLinks(source, color); + this.highlightExternalLinks( source, color ); } } } @@ -204,27 +208,27 @@ class CiteHighlighter { } } - highlightCitation(source, color) { + highlightCitation( source, color ) { // highlight whole cite // [title="source" i]... the "i" part is not working in :has() for some reason // use .toLowerCase() for now // using .addClass() instead of .css() or .attr('style') because I'm having issues getting medrs to override arXiv/Wikidata/other red sources - $('li[id^="cite_note-"]').has('a[href*="/'+source.toLowerCase()+'"]').addClass('cite-highlighter-' + color); - $('li[id^="cite_note-"]').has('a[href*=".'+source.toLowerCase()+'"]').addClass('cite-highlighter-' + color); + $( 'li[id^="cite_note-"]' ).has( 'a[href*="/' + source.toLowerCase() + '"]' ).addClass( 'cite-highlighter-' + color ); + $( 'li[id^="cite_note-"]' ).has( 'a[href*=".' + source.toLowerCase() + '"]' ).addClass( 'cite-highlighter-' + color ); } - highlightUnorderedListItem(source, color) { + highlightUnorderedListItem( source, color ) { // Also support any {{Cite}} template inside an unordered list. For example, a works cited section supporting a references section consisting of "Smith 1986, pp. 573-574" type citations. Example: https://en.wikipedia.org/wiki/C._J._Cregg#Articles_and_tweets - $('li').has('.citation a[href*="/'+source.toLowerCase()+'"]').addClass('cite-highlighter-' + color); - $('li').has('.citation a[href*=".'+source.toLowerCase()+'"]').addClass('cite-highlighter-' + color); + $( 'li' ).has( '.citation a[href*="/' + source.toLowerCase() + '"]' ).addClass( 'cite-highlighter-' + color ); + $( 'li' ).has( '.citation a[href*=".' + source.toLowerCase() + '"]' ).addClass( 'cite-highlighter-' + color ); } - highlightExternalLinks(source, color) { + highlightExternalLinks( source, color ) { // highlight external link only // !important; needed for highlighting PDF external links. otherwise the HTML that generates the PDF icon has higher specificity, and makes it transparent // [title="source" i]... the "i" means case insensitive. Default is case sensitive. - mw.util.addCSS('#bodyContent a[href*="/'+source+'" i] {background-color: '+this.colors[color]+' !important;}'); - mw.util.addCSS('#bodyContent a[href*=".'+source+'" i] {background-color: '+this.colors[color]+' !important;}'); + mw.util.addCSS( '#bodyContent a[href*="/' + source + '" i] {background-color: ' + this.colors[ color ] + ' !important;}' ); + mw.util.addCSS( '#bodyContent a[href*=".' + source + '" i] {background-color: ' + this.colors[ color ] + ' !important;}' ); } /** @@ -232,28 +236,28 @@ class CiteHighlighter { */ observeAndAddClassesToTooltips() { // TODO: switch from MutationObserver to mw.hook().add(). https://github.com/NovemLinguae/UserScripts/issues/167 - new MutationObserver(function () { - let el = document.getElementsByClassName('rt-tooltip')[0]; - if (el) { - for (let color in this.colors) { - if (typeof this.sources[color] === 'undefined') { + new MutationObserver( function () { + const el = document.getElementsByClassName( 'rt-tooltip' )[ 0 ]; + if ( el ) { + for ( const color in this.colors ) { + if ( typeof this.sources[ color ] === 'undefined' ) { continue; } - for (let source of this.sources[color]) { - if (this.wikicode.includes(source) || source === 'nih.gov' || source === 'twitter.com') { - if (source.includes('.') && !source.includes(' ')) { - $(el).has(`a[href*="${source.toLowerCase()}"]`).addClass('cite-highlighter-' + color); - $(el).has(`a[href*="${source.toLowerCase()}"]`).children().first().addClass('cite-highlighter-' + color); + for ( const source of this.sources[ color ] ) { + if ( this.wikicode.includes( source ) || source === 'nih.gov' || source === 'twitter.com' ) { + if ( source.includes( '.' ) && !source.includes( ' ' ) ) { + $( el ).has( `a[href*="${ source.toLowerCase() }"]` ).addClass( 'cite-highlighter-' + color ); + $( el ).has( `a[href*="${ source.toLowerCase() }"]` ).children().first().addClass( 'cite-highlighter-' + color ); } } } } } - }).observe(document.body, { + } ).observe( document.body, { subtree: false, - childList: true, - }); + childList: true + } ); } /** @@ -261,10 +265,10 @@ class CiteHighlighter { * anywhere in the URL. Example unreliableWord: blog. */ addHTMLClassesForUnreliableWords() { - for ( let word of this.unreliableWordsForOrangeHighlighting ) { - let color = 'unreliableWord'; - if ( this.wikicode.includes(word) ) { - $('li[id^="cite_note-"]').has('a[href*="'+word.toLowerCase()+'"]').addClass('cite-highlighter-' + color); + for ( const word of this.unreliableWordsForOrangeHighlighting ) { + const color = 'unreliableWord'; + if ( this.wikicode.includes( word ) ) { + $( 'li[id^="cite_note-"]' ).has( 'a[href*="' + word.toLowerCase() + '"]' ).addClass( 'cite-highlighter-' + color ); /* Too many false positives. Turning off for now. See https://github.com/NovemLinguae/UserScripts/issues/146 and https://github.com/NovemLinguae/UserScripts/issues/148 if ( window.citeHighlighterHighlightEverything ) { mw.util.addCSS('#bodyContent a[href*="'+word+'" i] {background-color: '+this.colors[color]+' !important;}'); @@ -276,31 +280,33 @@ class CiteHighlighter { /** * CAREFUL. This is case sensitive. + * @param {Object} haystack + * @param {...any} strings */ - deleteAll(haystack, ...strings) { - for ( let string of strings ) { - for ( let key in haystack ) { - haystack[key] = this.deleteFromArray(haystack[key], string); + deleteAll( haystack, ...strings ) { + for ( const string of strings ) { + for ( const key in haystack ) { + haystack[ key ] = this.deleteFromArray( haystack[ key ], string ); } } } - deleteFromArray(haystack, needle) { - const index = haystack.indexOf(needle); - if (index > -1) { - haystack.splice(index, 1); + deleteFromArray( haystack, needle ) { + const index = haystack.indexOf( needle ); + if ( index > -1 ) { + haystack.splice( index, 1 ); } return haystack; } - async getWikicode(title) { - let pageIsDeleted = ! mw.config.get('wgCurRevisionId'); + async getWikicode( title ) { + const pageIsDeleted = !mw.config.get( 'wgCurRevisionId' ); if ( pageIsDeleted ) { return ''; } - let api = new mw.Api(); - let response = await api.get( { + const api = new mw.Api(); + const response = await api.get( { action: 'parse', page: title, prop: 'wikitext', @@ -310,10 +316,10 @@ class CiteHighlighter { return response.parse.wikitext; } - async getWikicodeFromCache(title) { + async getWikicodeFromCache( title ) { // ForeignApi so that CiteHighlighter can be loaded on any wiki - let api = new mw.ForeignApi('https://en.wikipedia.org/w/api.php'); - let response = await api.get( { + const api = new mw.ForeignApi( 'https://en.wikipedia.org/w/api.php' ); + const response = await api.get( { action: 'query', prop: 'revisions', titles: title, @@ -324,18 +330,18 @@ class CiteHighlighter { smaxage: '86400', // cache for 1 day maxage: '86400' // cache for 1 day } ); - let wikicode = response.query.pages[0].revisions[0].slots.main.content; + const wikicode = response.query.pages[ 0 ].revisions[ 0 ].slots.main.content; return wikicode; } } // TODO: Idea from chlod: use mw.hook("wikipage.content").add( () => { rehiglight(); } ); instead. will listen for VE finishes saving or the page gets reloaded in any way. Gets called multiple times by accident sometimes though, so need to be careful not to apply duplicate classes to HTML elements. -$(async function() { +$( async function () { // TODO: I don't think I use mediawiki.Title. Remove that, and replace with mediawiki.Api? - await mw.loader.using(['mediawiki.util', 'mediawiki.Uri', 'mediawiki.Title', 'mediawiki.api'], async () => { - let ch = new CiteHighlighter(); + await mw.loader.using( [ 'mediawiki.util', 'mediawiki.Uri', 'mediawiki.Title', 'mediawiki.api' ], async () => { + const ch = new CiteHighlighter(); await ch.execute(); - }); -}); + } ); +} ); -// \ No newline at end of file +//