User

Difference between revisions of "Psubhashish/common.js"

< User:Psubhashish

(Created page with "→‎**************************************************************************: // Description: Adds a personal generator of words list to the RecordWizard // Usage: Copy thi...")
 
 
Line 1: Line 1:
/* ************************************************************************** */
+
// Define the function to remove punctuation and words to new line
// Description: Adds a personal generator of words list to the RecordWizard
+
function removePunctuationAndWords() {
// Usage: Copy this code into you User:Name/common.js, hack it as you need to.
+
// Get the input text from the textarea element
// See also : [[Help:Create a new generator]]
+
var inputText = document.getElementById("input-text").value;
 +
// Replace punctuation with a space, except for hyphen and apostrophe
 +
var textWithSpaces = inputText.replace(/[।“”\(\)\[\]\{\}<>.,\/#!$%\^&\*;:{}=\-_`~]+/g, " ").replace(/(^[-])|([-]$)/g, "");
  
'use strict';
+
// Convert spaces to new line breaks
 +
var textWithLineBreaks = textWithSpaces.replace(/[\s]+/g, "\n");
  
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'RecordWizard' ) {
+
// Set the processed text as output text in the textarea element
    mw.loader.using( ['oojs', 'oojs-ui', 'ext.recordWizard.generator'], function() {
+
document.getElementById("output-text").value = textWithLineBreaks;
    console.log('MediaWiki:Gadget-Demo.js');
+
}
        var rw = mw.recordWizard;
 
  
    /*
+
// Define the function to remove Latin character words
    * This first part allows the creation of a basic generator
+
function removeLatinWords() {
    * and allows the RecordWizard to find it (and thus, add a button in the UI for it)
+
// Get the output text from the textarea element
    *
+
var inputText = document.getElementById("output-text").value;
    * Replace everywhere in this file "rw.generator.Demo" by a custom
+
// Remove all characters except for the Bengali Unicode characters and space
    * and unique class name, but always in the "rw.generator" namespace.
+
var outputText = inputText.replace(/[^\u0B00-\u0B7F\s]+/g, "");
    * For example it can be: rw.generator.SomethingCool
 
    */
 
    rw.generator.Demo = function ( config ) {
 
    rw.generator.Generator.call( this, config );
 
    };
 
  
    OO.inheritClass( rw.generator.Demo, rw.generator.Generator );
+
// Trim the processed text and set it as output text in the textarea element
 +
document.getElementById("output-text").value = outputText.trim();
 +
}
  
    // This line defines an internal name for the generator
+
// Define the function to remove duplicates
    rw.generator.Demo.static.name = 'demo';
+
function removeDuplicates() {
 +
// Get the output text from the textarea element
 +
var inputText = document.getElementById("output-text").value;
 +
// Split the text into an array of words
 +
var words = inputText.split(/\s+/);
  
    // And this one defines the name for the generator which will be displayed in the UI
+
// Remove duplicate words and create a new array of unique words
    rw.generator.Demo.static.title = 'My Demo';
+
var uniqueWords = [];
 +
for (var i = 0; i < words.length; i++) {
 +
    if (uniqueWords.indexOf(words[i]) === -1) {
 +
        uniqueWords.push(words[i]);
 +
    }
 +
}
  
    /*
+
// Join the unique words into a string with new line breaks
    * This function should contain all the popup initialization stuff.
+
var outputText = uniqueWords.join("\n");
    * We create here for example two text fields, with a custom layout.
 
    * For more information, see OOui documentation:
 
    * https://www.mediawiki.org/wiki/OOUI
 
    */
 
    rw.generator.Demo.prototype.initialize = function () {
 
    // The two text fields
 
    this.aTextField = new OO.ui.TextInputWidget();
 
    this.anotherTextField = new OO.ui.TextInputWidget();
 
  
    // The custom layout
+
// Trim the processed text and set it as output text in the textarea element
    this.layout = new OO.ui.Widget( {
+
document.getElementById("output-text").value = outputText.trim();
    content: [
+
}
    new OO.ui.FieldLayout( this.aTextField, {
 
    align: 'top',
 
    label: 'This field do something cool.'
 
    }
 
    ),
 
    new OO.ui.FieldLayout(
 
    this.anotherTextField, {
 
    align: 'top',
 
    label: 'This one too!'
 
    }
 
    )
 
    ]
 
    } );
 
  
    // To be displayed, all the fields/widgets/... should be appended to "this.content.$element"
+
// Define the function to sort alphabetically
    this.content.$element.append( this.layout.$element );
+
function sortAlphabetically() {
 +
// Get the output text from the textarea element
 +
var inputText = document.getElementById("output-text").value;
 +
// Split the text into an array of words
 +
var words = inputText.split(/\s+/);
  
    // Do not remove this line, it will initialize the popup itself
+
// Sort the array of words alphabetically
    rw.generator.Generator.prototype.initialize.call( this );
+
words.sort();
    };
 
  
    /*
+
// Join the sorted words into a string with new line breaks
    * This function will be called when the user press the "Done" button.
+
var outputText = words.join("\n");
    *
 
    * Every words that you want to be added to the RecordWizard's word list
 
    * have to be added to an array called "this.list".
 
    *
 
    * The returned value can either be True, or if you want to do some asynchrone
 
    * stuff, you can return a jQuery promise
 
    */
 
    rw.generator.Demo.prototype.fetch = function () {
 
    // Get the values of our text fields
 
    var generator = this,
 
    demoText = this.aTextField.getValue(),
 
    anotherDemoText = this.anotherTextField.getValue();
 
  
    // Initialize a new promise
+
// Trim the processed text and set it as output text in the textarea element
    this.deferred = $.Deferred();
+
document.getElementById("output-text").value = outputText.trim();
 +
}
  
    // Initialize our word list
+
// Define the function to replace new line breaks with hash symbol
    this.list = [];
+
function replaceLinesWithHash() {
 +
// Get the output text from the textarea element
 +
var inputText = document.getElementById("output-text").value;
 +
// Replace new line breaks with hash symbol
 +
var outputText = inputText.replace(/\n+/g, "#");
  
    // We will do a asynchronous AJAX request to frwiki's API
+
// Trim the processed text and set it as output text in the textarea element
    this.frwikiApi = new mw.ForeignApi( 'https://fr.wikipedia.org/w/api.php', {
+
document.getElementById("output-text").value = outputText.trim();
    anonymous: true,
+
}
    parameters: { origin: '*' },
 
    ajax: { timeout: 10000 }
 
    } );
 
 
 
    // To get 10 random pages from the main namespace
 
    this.frwikiApi.get( {
 
    "action": "query",
 
    "format": "json",
 
    "list": "random",
 
    "rnnamespace": "0",
 
    "rnlimit": "10"
 
    } ).then( function( data ) {
 
    // We process here the result of our request
 
    // By adding each page title to our list
 
    var i;
 
    for ( var i=0; i < data.query.random.length; i++ ) {
 
    generator.list.push( data.query.random[ i ].title );
 
    }
 
 
 
    // And once we're done we can resolve our promise
 
    // So the process can end
 
    generator.deferred.resolve();
 
    } ).fail( function ( error ) {
 
    // If something goes wrong, we reject our promise
 
    // So the process stops and our popup shows the error message
 
    generator.deferred.reject( new OO.ui.Error( error ) );
 
    } );
 
  
    // At this point we're not done yet, make the dialog closing process
+
// Define the function to copy the output text to clipboard
    // to wait the promise to be resolved or rejected
+
function copyText() {
    return this.deferred.promise();
+
// Get the output text from the textarea element
    };
+
var outputText = document.getElementById("output-text");
    } );
+
// Select the text in the textarea element
 +
outputText.select();
 
}
 
}
 +
// Copy the selected text to clipboard
 +
document.execCommand("copy");

Latest revision as of 13:01, 14 March 2023

// Define the function to remove punctuation and words to new line
function removePunctuationAndWords() {
// Get the input text from the textarea element
var inputText = document.getElementById("input-text").value;
// Replace punctuation with a space, except for hyphen and apostrophe
var textWithSpaces = inputText.replace(/[।“”\(\)\[\]\{\}<>.,\/#!$%\^&\*;:{}=\-_`~]+/g, " ").replace(/(^[-])|([-]$)/g, "");

// Convert spaces to new line breaks
var textWithLineBreaks = textWithSpaces.replace(/[\s]+/g, "\n");

// Set the processed text as output text in the textarea element
document.getElementById("output-text").value = textWithLineBreaks;
}

// Define the function to remove Latin character words
function removeLatinWords() {
// Get the output text from the textarea element
var inputText = document.getElementById("output-text").value;
// Remove all characters except for the Bengali Unicode characters and space
var outputText = inputText.replace(/[^\u0B00-\u0B7F\s]+/g, "");

// Trim the processed text and set it as output text in the textarea element
document.getElementById("output-text").value = outputText.trim();
}

// Define the function to remove duplicates
function removeDuplicates() {
// Get the output text from the textarea element
var inputText = document.getElementById("output-text").value;
// Split the text into an array of words
var words = inputText.split(/\s+/);

// Remove duplicate words and create a new array of unique words
var uniqueWords = [];
for (var i = 0; i < words.length; i++) {
    if (uniqueWords.indexOf(words[i]) === -1) {
        uniqueWords.push(words[i]);
    }
}

// Join the unique words into a string with new line breaks
var outputText = uniqueWords.join("\n");

// Trim the processed text and set it as output text in the textarea element
document.getElementById("output-text").value = outputText.trim();
}

// Define the function to sort alphabetically
function sortAlphabetically() {
// Get the output text from the textarea element
var inputText = document.getElementById("output-text").value;
// Split the text into an array of words
var words = inputText.split(/\s+/);

// Sort the array of words alphabetically
words.sort();

// Join the sorted words into a string with new line breaks
var outputText = words.join("\n");

// Trim the processed text and set it as output text in the textarea element
document.getElementById("output-text").value = outputText.trim();
}

// Define the function to replace new line breaks with hash symbol
function replaceLinesWithHash() {
// Get the output text from the textarea element
var inputText = document.getElementById("output-text").value;
// Replace new line breaks with hash symbol
var outputText = inputText.replace(/\n+/g, "#");

// Trim the processed text and set it as output text in the textarea element
document.getElementById("output-text").value = outputText.trim();
}

// Define the function to copy the output text to clipboard
function copyText() {
// Get the output text from the textarea element
var outputText = document.getElementById("output-text");
// Select the text in the textarea element
outputText.select();
}
// Copy the selected text to clipboard
document.execCommand("copy");