User

Difference between revisions of "Yug/common.js"

< User:Yug

 
(11 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
// ==UserScript==
 
// ==UserScript==
 
// @name        Wikidata Editor
 
// @name        Wikidata Editor
(function() {
+
var entityId = "Q170146";   // Entity to update (Q170146)
    var entityId = "Q170146";   // Your Wikidata entity ID (QID)
+
var propertyId = "P4";     // Property to update (P4)
    var propertyId = "P4";   // Property ID (P4)
+
var newValue = "Q359";     // Value to set (Q359)
    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();
  
    // Fetch current data
+
api.get( params ).done( function ( data ) {
    fetch("https://lingualibre.org/api?action=wbgetentities&format=json&ids=" + entityId)
+
csrfToken = data.query.tokens.csrftoken;
        .then(function(response) {
+
console.log( csrfToken );
            return response.json();
+
} );
        })
 
        .then(function(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", "359")
 
                                },
 
                                type: "wikibase-entityid"
 
                            }
 
                        },
 
                        rank: "normal",
 
                        references: [],
 
                    }
 
                ];
 
  
                // Submit changes
+
console.log( "token2", csrfToken );
                fetch("https://lingualibre.org/api?action=wbeditentity&format=json", {
+
                    method: "POST",
+
// Define the API endpoint
                    body: JSON.stringify({
+
var apiUrl = "https://lingualibre.org/api.php";
                        id: entityId,
+
 
                        data: JSON.stringify(data.entities[entityId]),
+
// Define the edit summary
                        summary: "Updating property P4",
+
var editSummary = "Updating property P4";
                    }),
+
 
                    headers: {
+
// Create an object with the data to update
                        "Content-Type": "application/x-www-form-urlencoded",
+
var postData = {
                    },
+
  action: "wbsetclaimvalue",
                })
+
  format: "json",
                .then(function(response) {
+
    snaktype: "value",
                    return response.json();
+
  claim: JSON.stringify({
                })
+
    entity: entityId,
                .then(function(responseData) {
+
    property: propertyId,
                    console.log("Edit response:", responseData);
+
    snaktype: "value",
                })
+
    value: {
                .catch(function(error) {
+
      "entity-type": "item",
                    console.error("An error occurred:", error);
+
//    "value": numericId ,
                });
+
      "numeric-id": numericId
            }
+
    }
        })
+
  }),
        .catch(function(error) {
+
  summary: editSummary,
            console.error("An error occurred:", error);
+
  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);
 +
  }
 +
});

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);
  }
});