User

Yug/common.js

< User:Yug
Revision as of 14:23, 2 October 2023 by Yug (talk | contribs)

Note: After saving, 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: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
// Modules loader, conditional
// Sound library
if(/^User:Elfix\/LinguaLibre:Explore_the_sound_library(\/[a-z_-]+)?$/.test( mw.config.get( 'wgPageName' ) ) ) {
 mw.loader.load( '/index.ptitle=User:Elfix/MediaWiki:SoundLibrary.js&action=raw&ctype=text/javascript'); 
}


// ==UserScript==
// @name         Wikidata Editor
// @namespace    https://example.com
// @version      1.0
// @description  Edit a Wikidata page
// @include      https://www.wikidata.org/wiki/Q*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    const entityId = "Q170146"; // Your Wikidata entity ID (QID)
    const propertyId = "P4"; // Property ID (P407 for language of work or name)
    const newValue = "Q359"; // Value to set (Q356134 for English)

    // Fetch current data
    GM_xmlhttpRequest({
        method: "GET",
        url: "https://lingualibre.org/api.php?action=wbgetentities&format=json&ids="+entityId,
        onload: function(response) {
            const data = JSON.parse(response.responseText);
            if (data.success) {
                // Modify data
                data.entities[entityId].claims[propertyId] = [
                    {
                        mainsnak: {
                            snaktype: "value",
                            property: propertyId,
                            datavalue: {
                                value: {
                                    "entity-type": "item",
                                    "numeric-id": newValue.replace("Q", "")
                                },
                                type: "wikibase-entityid"
                            }
                        },
                        rank: "normal",
                        references: [],
                    }
                ];

                // Submit changes
                GM_xmlhttpRequest({
                    method: "POST",
                    url: "https://lingualibre/api.php?action=wbeditentity&format=json",
                    data: JSON.stringify({
                        id: entityId,
                        data: JSON.stringify(data.entities[entityId]),
                        summary: "Updating language of work or name",
                    }),
                    headers: {
                        "Content-Type": "application/x-www-form-urlencoded",
                    },
                    onload: function(response) {
                        console.log("Edit response:", response.responseText);
                    }
                });
            }
        }
    });
})();