User:So9q/openalex-doi-link.js

From Wikidata
Jump to navigation Jump to search

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/*
 * Simple script to add a link to the OpenAlex API
 * Forked from https://www.wikidata.org/wiki/User:Fnielsen/scholia-link.js by Fnielsen.
 * Getting the lemma with code by Salgo60 from https://www.wikidata.org/wiki/User:Salgo60/SAOB.js 
 */

( function ( mw, $ ) {
	if (mw.config.get( 'wgNamespaceNumber' ) != 0) {
		return;
	} else {
		mw.hook("wikibase.entityPage.entityLoaded").add( function ( e ) {
			for (key in e["claims"]){
				// We only care about DOI
				if (key == "P356") {
					if (e["claims"]["P356"].len > 1) {
						console.log("crossref-link.js: Error got more than 1 DOI")
					}
					else{
						for (key in e["claims"]["P356"][0]["mainsnak"]["datavalue"]){
					    	console.log( key + ": " + e["claims"]["P356"][0]["mainsnak"]["datavalue"][key]);
						}
						let doi = e["claims"]["P356"][0]["mainsnak"]["datavalue"]["value"]
						// Add portlet link for item
						mw.util.addPortletLink(
							'p-tb',
							'//api.openalex.org/works/doi:' + doi,
							'Lookup DOI in OpenAlex',
							't-openalex',
							'Try finding this Wikidata item in the OpenAlex JSON API using the DOI'
						);
					}
				}
			}
		});
	}
}( mediaWiki, jQuery ) );