User

Difference between revisions of "Yug/common.js"

< User:Yug

 
(14 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
  mw.loader.load( '/index.ptitle=User:Elfix/MediaWiki:SoundLibrary.js&action=raw&ctype=text/javascript');  
 
  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==
 
  
 
// ==UserScript==
 
// ==UserScript==
 
// @name        Wikidata Editor
 
// @name        Wikidata Editor
// @namespace    https://example.com
+
var entityId = "Q170146";  // Entity to update (Q170146)
// @version     1.0
+
var propertyId = "P4";     // Property to update (P4)
// @description  Edit a Wikidata page
+
var newValue = "Q359";     // Value to set (Q359)
// @include     https://www.wikidata.org/wiki/Q*
+
var numericId = Number(newValue.replace("Q", ""));
// @grant        none
+
var csrfToken = mw.user.tokens.get("csrfToken");
// ==/UserScript==
+
var params = {
 
+
action: 'query',
(function() {
+
meta: 'tokens',
    'use strict';
+
type: 'csrf',
 +
format: 'json'
 +
},
 +
api = new mw.Api();
  
    // Your Wikidata entity ID (QID)
+
api.get( params ).done( function ( data ) {
    var entityId = "Q170146";
+
csrfToken = data.query.tokens.csrftoken;
 +
console.log( csrfToken );
 +
} );
  
    // Property ID (P4)
+
console.log( "token2", csrfToken );
    var propertyId = "P4";
+
 +
// Define the API endpoint
 +
var apiUrl = "https://lingualibre.org/api.php";
  
    // Value to set (Q359)
+
// Define the edit summary
    var newValue = "Q359";
+
var editSummary = "Updating property P4";
  
    // Fetch current data
+
// Create an object with the data to update
    fetch("https://lingualibre.org/api?action=wbgetentities&format=json&ids=" + entityId)
+
var postData = {
        .then(function(response) {
+
  action: "wbsetclaimvalue",
            return response.json();
+
  format: "json",
        })
+
    snaktype: "value",
        .then(function(data) {
+
  claim: JSON.stringify({
            if (data.success) {
+
    entity: entityId,
                // Modify data
+
    property: propertyId,
                data.entities[entityId].claims[propertyId] = [
+
    snaktype: "value",
                    {
+
    value: {
                        mainsnak: {
+
      "entity-type": "item",
                            snaktype: "value",
+
//    "value": numericId ,
                            property: propertyId,
+
      "numeric-id": numericId
                            datavalue: {
+
    }
                                value: {
+
  }),
                                    "entity-type": "item",
+
  summary: editSummary,
                                    "numeric-id": newValue.replace("Q", "")
+
  token:csrfToken
                                },
+
};
                                type: "wikibase-entityid"
 
                            }
 
                        },
 
                        rank: "normal",
 
                        references: [],
 
                    }
 
                ];
 
  
                // Submit changes
+
// Send a POST request to update the item
                fetch("https://lingualibre.org/api?action=wbeditentity&format=json", {
+
$.post(apiUrl, postData, function(data) {
                    method: "POST",
+
  if (data.success) {
                    body: JSON.stringify({
+
    console.log("Item updated successfully!");
                        id: entityId,
+
  } else {
                        data: JSON.stringify(data.entities[entityId]),
+
    console.error("Error updating item:", data.error.info);
                        summary: "Updating property P4",
+
  }
                    }),
+
});
                    headers: {
 
                        "Content-Type": "application/x-www-form-urlencoded",
 
                    },
 
                })
 
                .then(function(response) {
 
                    return response.json();
 
                })
 
                .then(function(responseData) {
 
                    console.log("Edit response:", responseData);
 
                })
 
                .catch(function(error) {
 
                    console.error("An error occurred:", error);
 
                });
 
            }
 
        })
 
        .catch(function(error) {
 
            console.error("An error occurred:", error);
 
        });
 
})();
 

Latest revision as of 16:01, 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
var entityId = "Q170146";   // Entity to update (Q170146)
var propertyId = "P4";      // Property to update (P4)
var newValue = "Q359";      // Value to set (Q359)
var numericId = Number(newValue.replace("Q", ""));
var csrfToken = mw.user.tokens.get("csrfToken");
var params = {
		action: 'query',
		meta: 'tokens',
		type: 'csrf',
		format: 'json'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	csrfToken = data.query.tokens.csrftoken;
	console.log( csrfToken );
} );

	console.log( "token2", csrfToken );
	
// Define the API endpoint
var apiUrl = "https://lingualibre.org/api.php";

// Define the edit summary
var editSummary = "Updating property P4";

// Create an object with the data to update
var postData = {
  action: "wbsetclaimvalue",
  format: "json",
    snaktype: "value",
  claim: JSON.stringify({
    entity: entityId,
    property: propertyId,
    snaktype: "value",
    value: {
      "entity-type": "item",
 //     "value": numericId ,
      "numeric-id": numericId 
    }
  }),
  summary: editSummary,
  token:csrfToken
};

// Send a POST request to update the item
$.post(apiUrl, postData, function(data) {
  if (data.success) {
    console.log("Item updated successfully!");
  } else {
    console.error("Error updating item:", data.error.info);
  }
});