[Greasemonkey] Check COinS against OpenURL Resolver via GM_xmlhttpRequest

Ross Singer ross.singer at library.gatech.edu
Fri Dec 30 00:17:39 EST 2005


Matthew, this is slightly OT for GM, but, depending on your link 
resolver vendor, you may be able to send all your ContextObjects to the 
server at once.

It's personal preference, of course, but it would reduce the massive 
amount of unnecessary requests to the link resolver.

You can contact me off-list if you're interested (I've made GM scripts 
that do both).

-Ross.

On Dec 29, 2005, at 1:31 PM, Matthew Bachtell wrote:

> Hello all,
>
> I am trying to activate COinS, http://ocoins.info/, on pages but first 
> have them check against our OpenURL resolver before displaying an 
> image.  I don't want to display a link/image unless my library has 
> rights to read the article.
> Anyways my problem is that this works but it only works for the last 
> COinS in a given loop. So is it illegal to call GM_xmlhttpRequest 
> multiple times in a loop and have it execute itself each time it meets 
> a given qualification?  Or am I just way off in accomplishing this?
>
> Here is the code:
>
> // ==UserScript==
> // @name          COinS Activator
> // @description   Activates OpenURL COinS available for NIST 
> researchers
> // @include       http://*
> // ==/UserScript==
>
> var links = document.evaluate("//span[contains(@class,'Z3988')]",
>    document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
> if (!links) return;
>
> // start configuration
>
> var baseURL = 'http://nist-resolver.sirsi.net';
> var linkText = 'Article is available from the NIST Research Library';
> var linkImage = 'http://nvl.nist.gov/emplibrary/findatNIST.jpg';
>
> var articleAvailable = /Find article/;
>
> // end configuration
>
> for (var i = 0; i < links.snapshotLength; i++) {
>
>    var e = links.snapshotItem(i);
>    if (e.className.match(/\bZ3988\b/)) {
>
>    // do an GM_xmlhttpRequest to see if resource is available to NIST 
> researchers
>    GM_xmlhttpRequest({
>        method: 'GET',
>        url: baseURL + '?' + e.title.replace(/ctx_ver/, 'url_ver') + 
> '&url_ctx_fmt=ori:fmt:kev:mtx:ctx',
>        onload: function(results) {
>                page = results.responseText;
>                              if (articleAvailable.test(page)){
>                    var a = document.createElement('a');
>                    a.href = baseURL + '?' + e.title.replace(/ctx_ver/, 
> 'url_ver') +
>                        '&url_ctx_fmt=ori:fmt:kev:mtx:ctx';
>                              if (linkImage){
>                        var button = document.createElement('img');
>                        button.setAttribute('src', linkImage);
>                        button.setAttribute('alt', linkText);
>                        button.setAttribute('title', linkText);
>                        button.setAttribute('border', '0');
>                        a.appendChild(button);
>                    }
>                    else{
>                        a.innerHTML = linkText;
>                    }
>                              e.innerHTML = '';
>                    e.appendChild(a);
>                } // end if statement checking GM_xmlhttpRequest 
> response
>        } // end function
>    }); // end GM_xmlhttpRequest
>    } // end if statement that is checking for Z3988
> } // end for loop
>
> Thanks,
> Matt
>
> -- 
> Matthew Bachtell
>
>
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey
>



More information about the Greasemonkey mailing list