Jump to content
Wikipedia The Free Encyclopedia

User:Polygnotus/Scripts/DetectPromo-v2.js

From Wikipedia, the free encyclopedia
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Polygnotus/Scripts/DetectPromo-v2.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
 //Forked from [[User:Novem Linguae/Scripts/DetectPromo.js]]
 //now highlights the words in the article text and those in the top bar are now clickable

 // <nowiki>

 /*
 - Let reviewer know when certain promotional and POV keywords are detected.
 - Displays a bar at the top of the article, listing the detected keywords.
 - Highlights detected promotional words with a yellow background and red border within the article text.
 - Makes the words in the top bar clickable, scrolling to their first occurrence in the article.
 - Added "Report false positive" link to allow users to report incorrect detections.
 - Added confirmation dialog explaining false positive criteria before reporting.
 - Ignores promotional words that appear in the page title.
 - Ignores promotional words that appear in wiki link titles.
 */



 classDetectPromo{
 /** @type {string[]} */
 wordsToSearch=[
 '% growth','6-figure','7-figure','8-figure','9-figure',
 'B2B','B2C','a record','acclaimed','accomplished',
 'are a necessity','around the world','award winning','award-winning',
 'beloved','best available','bestselling','boasts','comprehensive',
 'countless hours','create a revolution','critical acclaim',
 'disrupt','drastically','dynamic','elevate','eminent','engaging',
 'entrepreneur','evangelist','excelled','exceptional','exemplified',
 'exemplify','expert','expertise','extensive','famous','fascinating',
 'fast growing','fast-growing','fastest growing','fastest-growing',
 'finest','fully integrated','fully-integrated','globally',
 'globally recognized','growing popularity','highlights',
 'highly accomplished','highly praised','highly specialized',
 'historic','honored with','hypnotic','illustrious','impressive',
 'indelible','inexhaustible','influential','innovation','innovative',
 'insights','inspired by','integrate','invaluable','leader in',
 'leading','legendary','leverage','massive','mastermind','more than',
 'most highly','most important','most impressive','most notable',
 'mystical','natural charm','noteworthy','numerous','organically',
 'outstanding','perfect','philanthropist','picturesque','pioneer',
 'pioneering','popular destination','popularity','premiere',
 'prestigious','prominence','prominent','promising','promulgator',
 'ranked','reinvent','remarkable','renowed','renowned','resonating',
 'respected','revolutionary','rising star','save millions','savvy',
 'seamless','sensual','several offers','showcased','signature',
 'significant','soulful','spanning','state of art','state of the art',
 'state-of-art','state-of-the-art','striking','super famous',
 'tailored','tranquility','transcend','transform','underpin',
 'ventured into','very first','visionary','wide selection',
 'widely used','world class','world-class','worldwide','zero to hero'
 ];

 /** 
  * Lists of allowed exception phrases - these will NOT be flagged even if they contain a listed word
  * Key is the base word, value is an array of exception phrases
  * @type {Object<string, string[]>} 
  */
 exceptionPhrases={
 'outstanding':['outstanding debts'],
 'drastically':['vary drastically'],
 'revolutionary':['revolutionary war'],
 'revolutionary':['American revolutionary'],
 'Revolutionary':['Revolutionary Committee'],
 'dynamic':['dynamic list'],
 'inspired':['inspired by'],
 'leading':['leading to'],
 'outstanding':['outstanding warrant'],
 'numerous':['numerous witnesses'],
 'significant':['significant blow'],
 'historic':['National Register of Historic Places']
 };

 /**
  * @param {Object} mw
  * @param {jQuery} $
  */
 constructor(mw,$){
 this.mw=mw;
 this.$=$;

 // Store page title for later use
 this.pageTitle=this.mw.config.get('wgTitle');
 this.pageName=this.mw.config.get('wgPageName');

 // Convert to lowercase for case-insensitive comparison
 this.pageTitleLower=this.pageTitle.toLowerCase();

 // Pre-process the words list for more efficient matching
 this.processWordsList();
 }

 /**
  * Process words list to identify base words for exception checking
  */
 processWordsList(){
 // Create a mapping of base words to their searchable variations
 this.baseWordMap={};

 // For each searchable word, find if it has a base word with exceptions
 for(constwordofthis.wordsToSearch){
 for(constbaseWordinthis.exceptionPhrases){
 // If the word contains the base word, map it back
 if(word.toLowerCase().includes(baseWord.toLowerCase())){
 if(!this.baseWordMap[word.toLowerCase()]){
 this.baseWordMap[word.toLowerCase()]=[];
 }
 this.baseWordMap[word.toLowerCase()].push(baseWord.toLowerCase());
 }
 }
 }
 }

 asyncexecute(){
 if(!this.shouldRunOnThisPage()){
 return;
 }

 consttitle=this.mw.config.get('wgPageName');
 constwikicode=awaitthis.getWikicode(title);
 if(!wikicode)return;

 // Clean wikicode by removing links and references
 constcleanedWikicode=this.cleanWikicode(wikicode);
 constsearchResults=this.getSearchResults(cleanedWikicode);

 if(searchResults.length>0){
 this.displayResults(searchResults);
 this.highlightPromoWords(searchResults);
 }
 }

 /**
  * @param {string[]} searchResults
  */
 displayResults(searchResults){
 constMAX_DISPLAYED_RESULTS=20;
 constdisplayedResults=searchResults.slice(0,MAX_DISPLAYED_RESULTS);

 lethtml=`
  <div id="DetectPromo" style="background-color: #ccc; padding: 10px; margin-bottom: 10px; display: flex; justify-content: space-between; align-items: center;">
  <div>
  <span style="font-weight: bold;">Potentially promotional words detected:</span> 
  `;

 html+=displayedResults.map(word=>
 `<a href="#" class="promo-word" data-word="${word}" style="color: blue; text-decoration: underline; cursor: pointer;">${word}</a>`
 ).join(', ');

 if(searchResults.length>MAX_DISPLAYED_RESULTS){
 html+=', ...... and more.';
 }

 html+=`</div>
  <div>
  <a href="#" id="report-false-positive" style="color: #d33; text-decoration: underline; font-size: 0.9em;">Report false positive</a>
  </div>
  </div>`;

 this.$('#contentSub').after(html);

 // Add click event listeners for promo words
 this.$('.promo-word').on('click',(e)=>{
 e.preventDefault();
 constword=this.$(e.target).data('word');
 this.scrollToWord(word);
 });

 // Add click event listener for false positive reporting
 this.$('#report-false-positive').on('click',(e)=>{
 e.preventDefault();
 this.showFalsePositiveDialog(searchResults);
 });
 }

 /**
  * Scroll to the first occurrence of a word in the article
  * @param {string} word
  */
 scrollToWord(word){
 constcontent=this.$('#mw-content-text');
 constregex=newRegExp(`\\b${this.escapeRegEx(word)}\\b`,'i');
 constelements=content.find('*').contents().filter(function(){
 returnthis.nodeType===Node.TEXT_NODE&&regex.test(this.textContent);
 });

 if(elements.length>0){
 constfirstOccurrence=elements[0];
 firstOccurrence.parentElement.scrollIntoView({behavior:'smooth',block:'center'});
 }
 }

 /**
  * Highlight promotional words with yellow background and red border within the article text
  * @param {string[]} searchResults
  */
 highlightPromoWords(searchResults){
 constcontent=this.$('#mw-content-text');
 consthighlightStyle='background-color: yellow; border: 1px solid red; padding: 2px; margin: -2px;';

 searchResults.forEach(word=>{
 constregex=newRegExp(`\\b${this.escapeRegEx(word)}\\b`,'gi');
 content.find('*').contents().filter(function(){
 returnthis.nodeType===Node.TEXT_NODE;
 }).each((_,textNode)=>{
 consttext=textNode.textContent;
 if(regex.test(text)){
 constnewHtml=text.replace(regex,`<span class="promo-highlight" data-word="${word}" style="${highlightStyle}">$&</span>`);
 constnewElement=document.createElement('span');
 newElement.innerHTML=newHtml;
 textNode.parentNode.replaceChild(newElement,textNode);
 }
 });
 });
 }

 /**
  * Check if a word appears in the page title
  * @param {string} word - The word to check
  * @return {boolean} - True if the word is in the page title
  */
 isWordInPageTitle(word){
 constwordLower=word.toLowerCase();

 // Simple check for exact word in title
 if(this.pageTitleLower.includes(wordLower)){
 // Check with word boundaries to ensure it's a complete word
 constwordRegex=newRegExp(`\\b${this.escapeRegEx(wordLower)}\\b`,'i');
 returnwordRegex.test(this.pageTitleLower);
 }

 returnfalse;
 }

 /**
  * Scans text for promotional words while respecting exceptions
  * @param {string} text - The wikicode text to search
  * @return {string[]} - List of found promotional words
  */
 getSearchResults(text){
 constresults=[];
 constlowerText=text.toLowerCase();

 // For each word to search
 for(constwordofthis.wordsToSearch){
 constlowerWord=word.toLowerCase();

 // Check if this word is in the text (quick filter)
 if(!lowerText.includes(lowerWord)){
 continue;
 }

 // Test with proper word boundaries
 constwordRegex=newRegExp(`\\b${this.escapeRegEx(word)}\\b`,'i');
 if(!wordRegex.test(text)){
 continue;
 }

 // Check if word is in page title
 if(this.isWordInPageTitle(word)){
 continue;// Skip this word if it's in the page title
 }

 // See if this word has any base words with exceptions
 constbaseWords=this.baseWordMap[lowerWord]||[];

 // Check if this is an exact match for an exception phrase
 letisException=false;

 for(constbaseWordofbaseWords){
 constexceptionList=this.exceptionPhrases[baseWord]||[];

 for(constexceptionPhraseofexceptionList){
 // If this word is part of an exception phrase that exists in the text, flag it
 if(exceptionPhrase.toLowerCase()===lowerWord||
 (lowerText.includes(exceptionPhrase.toLowerCase())&&
 newRegExp(`\\b${this.escapeRegEx(exceptionPhrase)}\\b`,'i').test(text))){
 isException=true;
 break;
 }
 }

 if(isException)break;
 }

 // Add it to results if it's not an exception
 if(!isException){
 results.push(word);
 }
 }

 returnresults;
 }

 /**
  * Clean wikicode by removing links and references
  * This focuses on removing wikilinks to avoid detecting promotional words
  * within link targets/titles
  * 
  * @param {string} wikicode
  * @return {string} cleanedWikicode
  */
 cleanWikicode(wikicode){
 // First, remove all wikilinks while keeping display text
 letcleanedCode=wikicode.replace(/\[\[([^\]\|]+)\|([^\]]+)\]\]/g,'2ドル');

 // Then, remove all simple wikilinks
 cleanedCode=cleanedCode.replace(/\[\[([^\]]+)\]\]/g,'1ドル');

 // Remove <ref> tags
 cleanedCode=cleanedCode.replace(/<ref[^<]*<\/ref>|<ref[^>]*\/>/gm,'');

 returncleanedCode;
 }

 /**
  * @return {boolean}
  */
 shouldRunOnThisPage(){
 constaction=this.mw.config.get('wgAction');
 constisDiff=this.mw.config.get('wgDiffNewId');
 constisDeletedPage=!this.mw.config.get('wgCurRevisionId');
 constnamespace=this.mw.config.get('wgNamespaceNumber');
 consttitle=this.mw.config.get('wgPageName');

 return(
 action==='view'&&
 !isDiff&&
 !isDeletedPage&&
 ([0,118].includes(namespace)||
 title==='User:Polygnotus')
 );
 }

 /**
  * @param {string} title
  * @return {Promise<string|null>} wikicode
  */
 asyncgetWikicode(title){
 try{
 constapi=newthis.mw.Api();
 constresponse=awaitapi.get({
 action:'parse',
 page:title,
 prop:'wikitext',
 formatversion:'2',
 format:'json'
 });
 returnresponse.parse.wikitext;
 }catch(error){
 console.error('Error fetching wikicode:',error);
 returnnull;
 }
 }

 /**
  * Show enhanced dialog with text field for reporting false positives
  * @param {string[]} detectedWords
  */
 showFalsePositiveDialog(detectedWords){
 // Use MediaWiki's OOjs UI dialog framework
 mw.loader.using(['oojs-ui-core','oojs-ui-windows','oojs-ui-widgets'],()=>{
 // Create layout for dialog content
 constlayout=newOO.ui.FieldsetLayout({
 label:'Report False Positive'
 });

 // Create a more direct text field for entering word combination
 constwordCombinationField=newOO.ui.TextInputWidget({
 placeholder:'Enter word combination (e.g., "outstanding warrant")',
 title:'Enter the exact multi-word combination that should not be flagged as promotional'
 });

 // Display the detected promotional words for reference
 constdetectedWordsHtml=$('<div>')
 .addClass('detected-words-list')
 .css({
 'margin-bottom':'10px',
 'background-color':'#f8f9fa',
 'padding':'8px',
 'border-radius':'2px',
 'border':'1px solid #eaecf0'
 })
 .append($('<strong>').text('Detected promotional words: '))
 .append(document.createTextNode(detectedWords.join(', ')));

 // Add fields to layout
 layout.addItems([
 newOO.ui.FieldLayout(wordCombinationField,{
 label:'Enter a word combination that is NOT promotional',
 align:'top'
 })
 ]);

 // Add the detected words info before the layout
 layout.$element.prepend(detectedWordsHtml);

 // Add instructions
 constinstructionsLayout=newOO.ui.PanelLayout({
 padded:true,
 expanded:false
 });

 instructionsLayout.$element.append(
 $('<div>')
 .css('margin-bottom','1em')
 .append($('<p>').text('Please provide a multi-word combination that includes one of the promotional words but is NOT promotional.'))
 .append($('<p>').html('<strong>The combination must:</strong>'))
 .append($('<ul>')
 .append($('<li>').text('Include at least 2 words'))
 .append($('<li>').text('Include one of the detected promotional words'))
 .append($('<li>').text('Form a phrase that is NOT promotional'))
 )
 .append($('<p>').text('For example:'))
 .append($('<ul>')
 .append($('<li>').text('"outstanding" can be promotional, but "outstanding warrant" is not'))
 .append($('<li>').text('"leading" can be promotional, but "leading to" is not'))
 .append($('<li>').text('"award-winning" can be promotional, but "award-winning ceremony" is not'))
 )
 );

 // Define a proper dialog class with a name
 functionFalsePositiveDialog(config){
 FalsePositiveDialog.super.call(this,config);
 }
 OO.inheritClass(FalsePositiveDialog,OO.ui.ProcessDialog);

 // Define static properties
 FalsePositiveDialog.static.name='falsePositiveDialog';
 FalsePositiveDialog.static.title='Report False Positive';
 FalsePositiveDialog.static.actions=[
 {
 action:'cancel',
 label:'Cancel',
 flags:['safe','close']
 },
 {
 action:'submit',
 label:'Submit Report',
 flags:['primary','progressive']
 }
 ];
 FalsePositiveDialog.static.size='medium';

 // Create message dialog
 constmessageDialog=newFalsePositiveDialog();

 // Define dialog process
 FalsePositiveDialog.prototype.getActionProcess=function(action){
 constself=this;

 if(action==='submit'){
 constwordCombination=wordCombinationField.getValue().trim();

 // Check if the word combination has at least 2 words
 constwordCount=wordCombination.split(/\s+/).filter(w=>w.length>0).length;

 if(wordCount<2){
 // Show error message for insufficient words
 wordCombinationField.setValidityFlag(false);
 returnnewOO.ui.Process(function(){
 self.showErrors(newOO.ui.Error(
 'Please enter a multi-word combination (at least 2 words). We only want to exclude word combinations that are not promotional in context.',
 {recoverable:true}
 ));
 });
 }

 // Check if any of the detected promotional words are part of the combination
 constcontainsPromoWord=detectedWords.some(word=>
 wordCombination.toLowerCase().includes(word.toLowerCase())
 );

 if(!containsPromoWord){
 // Show error message for missing any promotional word
 wordCombinationField.setValidityFlag(false);
 returnnewOO.ui.Process(function(){
 self.showErrors(newOO.ui.Error(
 'Your word combination must include one of the detected promotional words.',
 {recoverable:true}
 ));
 });
 }

 // Find which promotional word is included in the combination
 letincludedWord='';
 for(constwordofdetectedWords){
 if(wordCombination.toLowerCase().includes(word.toLowerCase())){
 includedWord=word;
 break;
 }
 }

 // All validation passed, proceed with submission
 returnnewOO.ui.Process(function(){
 self.close({
 action:action,
 selectedWord:includedWord,
 wordCombination:wordCombination
 });
 });
 }

 // Handle cancel and close actions explicitly
 if(action==='cancel'||action==='close'){
 returnnewOO.ui.Process(function(){
 self.close({action:action});
 });
 }

 // Fallback for other actions
 returnFalsePositiveDialog.super.prototype.getActionProcess.call(this,action);
 };

 // Define dialog setup process - this is the proper way to add content
 FalsePositiveDialog.prototype.getSetupProcess=function(){
 returnFalsePositiveDialog.super.prototype.getSetupProcess.call(this).next(function(){
 // Add content to the body
 this.$body.append(instructionsLayout.$element,layout.$element);
 },this);
 };

 // This section is now handled in the static properties

 // Add dialog to window manager
 constwindowManager=newOO.ui.WindowManager();
 this.$('body').append(windowManager.$element);
 windowManager.addWindows([messageDialog]);

 // Open dialog
 windowManager.openWindow(messageDialog).closed.then(data=>{
 if(data&&data.action==='submit'){
 this.reportFalsePositive(data.selectedWord,data.wordCombination);
 }
 });
 });
 }

 /**
  * Report false positive by redirecting to the talk page with the word combination
  * @param {string} selectedWord - The promotional word selected
  * @param {string} wordCombination - The non-promotional word combination
  */
 reportFalsePositive(selectedWord,wordCombination){
 constcurrentPage=this.mw.config.get('wgPageName');

 // Use exactly the same URL structure as DuplicateReferences
 constbaseUrl='https://en.wikipedia.org/wiki/User_talk:Polygnotus';
 constaction='edit';
 constsection='new';
 constpreloadtitle='Reporting%20%5B%5BUser%3APolygnotus%2FDetectPromo%7CDetectPromo%5D%5D%20false-positive';

 // Note the 1ドル placeholder in the preload parameter - this is key!
 constpreload='User:Polygnotus/1ドル';

 // Format the preloadparams with the word combination
 constpreloadparams=encodeURIComponent(`[[${currentPage}]] "${selectedWord}" in phrase "${wordCombination}" is not promotional ~~`+`~~`);

 // Construct the final URL to match the example format exactly
 constreportURL=`${baseUrl}?action=${action}&section=${section}&preloadtitle=${preloadtitle}&preload=${preload}&preloadparams%5b%5d=${preloadparams}`;

 // Redirect to the report page
 window.location.href=reportURL;
 }

 /**
  * @param {string} string
  * @return {string} escapedString
  */
 escapeRegEx(string){
 returnstring.replace(/[.*+?^${}()|[\]\\]/g,'\\$&');
 }
 }

 $(()=>{
 mw.loader.using(['mediawiki.api','oojs-ui-core','oojs-ui-windows','oojs-ui-widgets']).then(()=>{
 newDetectPromo(mw,$).execute();
 });
 });

 // </nowiki>

AltStyle によって変換されたページ (->オリジナル) /