Jump to content

User:Dfko/common.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
var accessIsAlreadyOpenPrefixes = new Array(
	'10.1098', '10.1155', '10.1186', '10.1371', '10.1534', '10.2196', '10.3352', '10.3389', '10.3390',
	'10.3814', '10.3897', '10.4061', '10.5194', '10.5402', '10.6064', '10.7167', '10.7554', '10.7717'
	);
var refSpanSelector = "span.citation.journal";
var doiRegex = /dx\.doi\.org\/(.*)/g;
var citationSpans = document.querySelectorAll(refSpanSelector);
var doiLinks = [];
var doiLinkElements = [];
for (i = 0; i < citationSpans.length; i++) {
	var links = citationSpans[i].querySelectorAll("a");
	for (j = 0; j < links.length; j++) {
		var link = decodeURIComponent(links[j].href);
		var match = doiRegex.exec(link);
		if (match !== null && accessIsAlreadyOpenPrefixes.indexOf(match[1].slice(0, 8)) == -1) {
			doiLinks.push(link);
			doiLinkElements.push(links[j]);
		}
	}
}

function oabutton(options) {
    var about = {
        version: 0.1,
        author: "Mark MacGillivray",
        created: "01102014",
        description: "A javascript client that operates against the Open Access Button 2.0 API"
    };
    var defaults = {
        api: 'http://oabutton.cottagelabs.com/api',
        api_key: ''
    };
    this.options = $.extend(defaults, options);
    this.response = {};
}

oabutton.prototype = {
    send: function(action,o) {
        this.options.api_key && !o.data.api_key ? o.data.api_key = this.options.api_key : false;
        var vars = {
            type: 'POST',
            url: this.options.api + '/' + action,
            contentType: 'application/json',
            dataType: 'JSON',
            processData: false,
            //crossDomain: true,
            cache: false,
            context: this,
            data: JSON.stringify(o.data)
        };
        vars.success = function(res) {
            this.response = res;
            if ( !this.options.api_key && res.api_key ) {
                this.options.api_key = res.api_key;
            }
            if ( !this.options.username && res.username ) {
                this.options.username = res.username;
            }
            typeof o.success == 'function' ? o.success(res) : false;
        };
        typeof o.error == 'function' ? vars.error = o.error : false;
        $.ajax(vars);
    },
    register: function(o) {
        // o should be an object containing a data object with email, profession, username
        // and also a success function and error function if required
        this.send('register',o);
    },
    status: function(o) {
        //this.send('status',o);
        // TODO: annoying behaviour of CORS on POST means this is being fugded as a JSONP GET for now
        this.options.api_key && !o.data.api_key ? o.data.api_key = this.options.api_key : false;
        var vars = {
            type: 'GET',
            url: this.options.api + '/status',
            cache: false,
            context: this,
            dataType: 'JSONP',
            data: o.data,
            success: function(res) {
                this.response = res;
                typeof o.success == 'function' ? o.success(res) : false;
            }
        };
        typeof o.error == 'function' ? vars.error = o.error : false;
        $.ajax(vars);
    },
    blocked: function(o,rid) {
        var t = 'blocked';
        if ( rid ) {
            t += '/' + rid;
        }
        this.send(t,o);
    },
    wishlist: function(o) {
        this.send('wishlist',o);
    }
    // TODO: add the processor API route
};

var oaicon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAAEEfUpiAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3gsZAwICYF9DDAAACOhJREFUWMONl2mMXMURx3/V/d7M7sxeMz4CxgdgfARb4cYkBCREQgghICDCgkiRkEIAe43B4fCFd8feZY2QhfHFYSlRIhFERA4ghAARh4CYKyEkXHFiYmODLzxj7+7Mzsx73ZUPs7v2eteO68PTvJ6uqn91Vf2rH/RLcXkm0Kj/pW95ZitApbPhNAMQWtkBEFXDjABEnRmtxlwfWmkfMIGuJQAw2r9QKYy5AEB0I/TtzN4bWJ0XNhUa0VYorBxfz6Gia/vVVmS0mMvm4q6s6jwMUHvIPPggR+DVTzLC+HLkTi+ObZoFIINWVma1IiUb+JQLFuXFdWW7N/fkmwY3FDsJQjKREdkulgn27rwMtTAbyjNabKzWNzyyT+WL2roBeHouJjotq4o8nrT6Wemm7F0cLtWOjAJ05zDalVVdigVqxwOQWFKQks90gfkYlE98bX1wQ7Uzq6XIpaw1J1diZXpFFLSGoZjL3BqJC+rr7S4rfgZqXpZV6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vaC0WAQMMxopzbf9glYBuAzgv/Dw+sA22tffalf7/usXJVn429+oTViYkg2NTTnj0PIJVqKSSXFq43i/INsjDfkFxauKLZNe+sfSmNOhvj/hZYO9GD76vqc3Fd4THdBkyMzFGpeICvP7wDGdUIx7Whvcubbw6tWW9FjPeKiOyKYn1B0VcU2YWAqh4nyIV1oVzivI6zRvCqvhrLbQ1t+bW6AMMpqMxBNTp4Mx65CPsnlz4KWhZjblVfzE0I1XRfFBhZol4urAtkuKZCX6wYMa+7OOoMSL2UbN9V9feLMXeqP9LAO+I0HOAEAJ2M8F2MrMOV2htFbGCcNwmxUROAurDbEFcV79NtPaqt2Mpz+LottSo41Nbh8j8sfULrXio9rgAAAABJRU5ErkJggg==";
var blockedicon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAANCAYAAACUwi84AAAABmJLR0QA/wD/AP+gvaeTAAABGElEQVQYlWWPIUsEYRiEn/nuPTyElSuL4C8wiGcQLt5fsNlMwvoLLIu4wl0wickmVoMmTYbjNmkR/4JgEQSTcLDf7mvZDeIDUwaGmRGAu2s6nR4B+5IE3OZ5fiXJewD9fv9c0pmkBfANHJdlmczn8ycVRTE0sy9Jh3me3wDMZrMDd78OIawHM9sEelVVPdAi6RHoufu2AQYwGAyqoig2AEIIP03TAKxal4oxLsxsBFDX9VvnB3cfAg6MOtPddyR5jHEtSNoD1HZ/AO/ddTObGLDbBqsQwrakJsb4Cay4+9gkLd0dwNx9q67rqhsOLM3d74ExoKZpSv5yF9I0vZD0wn+e0zS9DFmWVUmSTIBT4LXVSYxxkmVZ9QuaFG7xFQUbEgAAAABJRU5ErkJggg==";

// BEWARE the api key is hardcoded below.
var OaButtonApi = new oabutton({api_key: '44ab0972-3a56-4d1d-ba10-c98c5342ac1d'});
var oabuttonhref = "javascript:(function(){oabuid='44ab0972-3a56-4d1d-ba10-c98c5342ac1d';_my_script=document.createElement('SCRIPT');_my_script.type='text/javascript';_my_script.src='https://openaccessbutton.org/static/oabutton_includer.js?';document.getElementsByTagName('head')[0].appendChild(_my_script);}())";

doiLinkElements.forEach(function(e,i,a) {
	var data = {url: decodeURIComponent(e.href)};
	var successCallback = function(response) {
		var blockedCount = response.blocked;
		var wishCount = response.wishlist;
		var oaiconImg = document.createElement("img");
		if(blockedCount > 0) {
			oaiconImg.src = blockedicon;
		} else {
			oaiconImg.src = oaicon;
		}
		oaiconImg.height = 12; oaiconImg.width = 12;
		var oaiconAnchor = document.createElement("a");
		oaiconAnchor.href = oabuttonhref;
		oaiconAnchor.appendChild(oaiconImg);
		e.parentNode.appendChild(oaiconAnchor);
		var oaSpanNode = document.createElement("span");
		var oaSpanText = document.createTextNode(" Blocked reports: "+blockedCount+". Wishlisted "+wishCount+" times.");
		oaSpanNode.appendChild(oaSpanText);
		e.parentNode.appendChild(oaSpanNode);
	};
	OaButtonApi.status({data: data, success: successCallback, linkElement: e});
});