User:Tomodachi94/ping all users involved in discussion.js

From Wikidata
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// ==UserScript==
// @name         Ping all participants
// @namespace    https://floss.social/@tomodachi94
// @version      0.0.1
// @description  Ping all participants in a property proposal discussion. This script pings everyone who has left a message on a page; it ignores users who have used {{No ping}}/{{Don't ping}}. You can only ping users for a speciic discussion by pressing the 'edit' button that appears in specific sections. This script automatically hides itself if you're not editing a page; otherwise, it goes under the 'Tools' section of the sidebar.
// @author       [[User:Tomodachi94]]
// @downloadURL  https://www.wikidata.org/wiki/User:Tomodachi94/ping_all_users_involved_in_discussion.js?action=raw
// @updateURL    https://www.wikidata.org/wiki/User:Tomodachi94/ping_all_users_involved_in_discussion.js?action=raw
// @match        *://*.wikidata.org/*
// @grant        none
// ==/UserScript==

// TODO: Allow the user to configure regexes for URLs they want the script on.

var discussionPingerConfigUsername = discussionPingerConfigUsername || mw.user.getName();
var discussionPingerConfigUserLinkRegex = discussionPingerConfigUserLinkRegex || /\[\[User:([^|\]]+)/g;
var discussionPingerConfigNoPingTemplates = discussionPingerConfigNoPingTemplates || ["(?:D|d)on't ping", "(?:N|n)o ping"]

function pingAllUsersInvolvedInDiscussion() {
    'use strict';

    // Function to run when the button is clicked
    function pingAllUsers() {
        // Get the contents of the source code editor
        var editor = document.getElementById('wpTextbox1');
        var editorContents = editor.value;

        var usernames = [];
        
    	// Create a regular expression to match the no-ping templates and the entire line
    	var noPingRegex = new RegExp("^.*({{" + discussionPingerConfigNoPingTemplates.join("|") + ").*$", "gm");

    	// Remove any lines that match the no-ping templates
    	editorContents = editorContents.replace(noPingRegex, "");
    	
        var match;
        while ((match = discussionPingerConfigUserLinkRegex.exec(editorContents)) !== null) {
            // Get the username from the match
            var username = match[1];

            // Add the username to the array
            if (!usernames.includes(username || discussionPingerConfigUsername)) {
                // Add the username to the array
                usernames.push(username);
            }
        }

		if (usernames.length == 0 || typeof usernames == 'undefined') {
			mw.notification.notify("No users found", {autoHide: true, autoHideSeconds: 'short', tag: null, title: "Ping all users involved in discussion.js", type: "warn", visibleTimeout: true, id: false, classes: false});
			// alert("No users found")
		} else {
	        // Create the {{Ping}} template with multiple parameters
        	var pingTemplate = '{{Ping|' + usernames.join('|') + '}}';

        	// Append the template to the source code editor
        	var editorTextarea = document.getElementById('wpTextbox1');
        	editorTextarea.value += pingTemplate + ' ';
		}
    }

    // Create the button element
    var pingButton = document.createElement('li');
    pingButton.innerHTML = '<a href="#">Ping all users in discussion</a>';

    // Add the button to the Tools section of the sidebar
    var toolsList = document.querySelector('#p-tb ul');
    toolsList.appendChild(pingButton);

    // Add an event listener to the button
    pingButton.addEventListener('click', function(event) {
        event.preventDefault();
        pingAllUsers();
    });
}

/* Use MediaWiki's hooks system to only run the script when you edit a page. */
mw.hook( 'wikipage.editform' ).add(pingAllUsersInvolvedInDiscussion);