User:Lectrician1/add-required-qualifier.js

Note: After publishing, 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: Press Ctrl-F5.
// an attempt (not done)
// license: CC0

$(document).on('DOMSubtreeModified', '.ui-suggester-input', async function() {
  // Check if the class 'ui-entityselector-input-recognized' has been added
  if ($(this).hasClass('ui-entityselector-input-recognized')) {
    let propertyID = $(this).entityselector("selectedEntity").id
    let query = `
				SELECT ?requiredQualifier WHERE {
  wd:${propertyID} p:P2302 ?statement.
  ?statement ps:P2302 wd:Q21510856;
             pq:P2306 ?requiredQualifier.
}`
    let result = await $.post("https://query.wikidata.org/sparql?format=json", {
      query: query
    })
    
    for (let thing of result.results.bindings) {
      let qualifierID = thing.requiredQualifier.value.split("entity/")[1]
      
      
    $('.wikibase-toolbar-button-add > a').filter(function() {
      return $(this).closest('.wikibase-statementview-qualifiers').length > 0;
    }).click()
      
      setTimeout($(':focus').val(qualifierID), 100);
    }
  }
});