Item Labels

Show items labels in Wikidata Query Service web results view in the navigator user language without calling the Label Service in the sparql code

  1. // ==UserScript==
  2. // @name Item Labels
  3. // @name:fr Libellé des éléments
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.1.2.3
  6. // @description Show items labels in Wikidata Query Service web results view in the navigator user language without calling the Label Service in the sparql code
  7. // @description:fr Montre les libellés en anglais des éléments à coté de leur identifiant Q dans les résultats de recherche sans avoir à faire appel au service de libellé dans la requête, pour le Wikidata Query service.
  8. // @author TomT0m
  9. // @match https://query.wikidata.org/
  10. // @match https://commons-query.wikimedia.org/
  11. // @grant none
  12. // @license Creative Commons CC0 1.0 Universal
  13. // ==/UserScript==
  14.  
  15. (function($) {
  16. 'use strict';
  17.  
  18. const base_url="http://www.wikidata.org/"
  19. const entity_prefix = base_url + "entity/"
  20. let lang="en"
  21.  
  22. if (navigator.language) {
  23. lang = navigator.language.split(/-/)[0];
  24. }
  25.  
  26.  
  27. function gen_wdapi_queryurl(ids){
  28. let api = "https://www.wikidata.org/w/api.php?"
  29.  
  30. var searchParams = new URLSearchParams("");
  31. searchParams.set("action","wbgetentities")
  32. searchParams.set("sites","wikidatawiki")
  33. searchParams.set("ids",ids)
  34. searchParams.set("props", "labels")
  35.  
  36. searchParams.set("languages", lang)
  37. searchParams.set("format","json")
  38. searchParams.set("origin","*")
  39. return api + searchParams.toString()
  40. }
  41.  
  42. function listifyReqJson(json) {
  43. return Object.entries(json.entities)
  44. .filter(([k, v] , i) => v.labels[lang] ) // keep only entities with an actual label in language
  45. .map( ([k, v] , i) => [k, v.labels])
  46. }
  47.  
  48. function render_label(label){
  49. return $("<span/>", {
  50. "lang":label.language
  51. }).append(document.createTextNode(label.value));
  52. }
  53. const lclass = "itemLabels-labellized";
  54. const queried_class = "itemLabels-queried";
  55.  
  56. function LabellizeQitems () {
  57.  
  58. let links = $( '#query-result' ).find( `a.item-link:not(.${queried_class})` )
  59.  
  60. if (!links.length){return}
  61. if (links.attr("checked")) {return}
  62.  
  63. links.addClass(queried_class)
  64.  
  65. let ids = links.map(
  66. (o, v) => { if(/\/entity\/(.*)$/.exec(v.href)){
  67. return /\/entity\/(.*)$/.exec(v.href)[1]}
  68. else {return "K" }
  69. }
  70. ).filter((k,v) => v[0]=="Q")
  71. .toArray()
  72.  
  73. let plop = ""
  74.  
  75. while( ids.length > 0){
  76. fetch(gen_wdapi_queryurl(ids.splice(0,50).join("|")))
  77. .then(
  78. response => response.json()
  79. ).then(json => listifyReqJson(json).forEach(
  80. ([k, labels]) => {
  81. let link=$(`.item-link[href='${entity_prefix}${k}']:not(.${lclass})`)
  82. link.append(" – ").append(render_label(labels[lang]))
  83. link.addClass(lclass)
  84. }
  85. )
  86. )
  87. }
  88.  
  89. }
  90. setInterval(LabellizeQitems, 3000);
  91. })(window.$)