User

Difference between revisions of "Yug/common.js"

< User:Yug

Line 13: Line 13:
 
// @include      https://www.wikidata.org/wiki/Q*
 
// @include      https://www.wikidata.org/wiki/Q*
 
// @grant        GM_xmlhttpRequest
 
// @grant        GM_xmlhttpRequest
 +
// ==/UserScript==
 +
 +
// ==UserScript==
 +
// @name        Wikidata Editor
 +
// @namespace    https://example.com
 +
// @version      1.0
 +
// @description  Edit a Wikidata page
 +
// @include      https://www.wikidata.org/wiki/Q*
 +
// @grant        none
 
// ==/UserScript==
 
// ==/UserScript==
  
 
(function() {
 
(function() {
     const entityId = "Q170146"; // Your Wikidata entity ID (QID)
+
     'use strict';
     const propertyId = "P4"; // Property ID (P407 for language of work or name)
+
 
     const newValue = "Q359"; // Value to set (Q356134 for English)
+
    // Your Wikidata entity ID (QID)
 +
     const entityId = "Q170146";
 +
    // Property ID (P4)
 +
     const propertyId = "P4";
 +
    // Value to set (Q359)
 +
    const newValue = "Q359";
  
 
     // Fetch current data
 
     // Fetch current data
     GM_xmlhttpRequest({
+
     fetch("https://lingualibre.org/api.php?action=wbgetentities&format=json&ids="+ entityId)
        method: "GET",
+
    .then(response => response.json())
        url: "https://lingualibre.org/api.php?action=wbgetentities&format=json&ids="+entityId,
+
        .then(data => {
        onload: function(response) {
 
            const data = JSON.parse(response.responseText);
 
 
             if (data.success) {
 
             if (data.success) {
 
                 // Modify data
 
                 // Modify data
Line 47: Line 59:
  
 
                 // Submit changes
 
                 // Submit changes
                 GM_xmlhttpRequest({
+
                 return fetch("https://lingualibre.org/api.php?action=wbeditentity&format=json", {
 
                     method: "POST",
 
                     method: "POST",
                     url: "https://lingualibre/api.php?action=wbeditentity&format=json",
+
                     body: JSON.stringify({
                    data: JSON.stringify({
 
 
                         id: entityId,
 
                         id: entityId,
 
                         data: JSON.stringify(data.entities[entityId]),
 
                         data: JSON.stringify(data.entities[entityId]),
                         summary: "Updating language of work or name",
+
                         summary: "Updating property P4",
 
                     }),
 
                     }),
 
                     headers: {
 
                     headers: {
 
                         "Content-Type": "application/x-www-form-urlencoded",
 
                         "Content-Type": "application/x-www-form-urlencoded",
 
                     },
 
                     },
                    onload: function(response) {
 
                        console.log("Edit response:", response.responseText);
 
                    }
 
 
                 });
 
                 });
 
             }
 
             }
         }
+
         })
    });
+
        .then(response => response.json())
 +
        .then(responseData => {
 +
            console.log("Edit response:", responseData);
 +
        })
 +
        .catch(error => {
 +
            console.error("An error occurred:", error);
 +
        });
 
})();
 
})();

Revision as of 14:28, 2 October 2023

// 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==

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

(function() {
    'use strict';

    // Your Wikidata entity ID (QID)
    const entityId = "Q170146";
    // Property ID (P4)
    const propertyId = "P4";
    // Value to set (Q359)
    const newValue = "Q359";

    // Fetch current data
    fetch("https://lingualibre.org/api.php?action=wbgetentities&format=json&ids="+ entityId) 
    .then(response => response.json())
        .then(data => {
            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
                return fetch("https://lingualibre.org/api.php?action=wbeditentity&format=json", {
                    method: "POST",
                    body: JSON.stringify({
                        id: entityId,
                        data: JSON.stringify(data.entities[entityId]),
                        summary: "Updating property P4",
                    }),
                    headers: {
                        "Content-Type": "application/x-www-form-urlencoded",
                    },
                });
            }
        })
        .then(response => response.json())
        .then(responseData => {
            console.log("Edit response:", responseData);
        })
        .catch(error => {
            console.error("An error occurred:", error);
        });
})();