3

Our university provides a proxy service that allows me to browse websites as if I was online via the university network. However, lazy as I am, I get tired of going to the URL edit field in Firefox and change the https://superuser.com/ to http://superuser.com.ezproxy.its.uu.se/ and open that new URL.

Instead, I much rather like to just click a button in my Firefox window. Therefore, my question is, how can I create such a functionality. I am happy with userscript, ubiquity, or an add-on as solution: what Firefox functionality should I use for the task of appending ezproxy.its.uu.se to the domain part of any URL, and open that new URL?

4 Answers4

9

Change the location by saving the following as a bookmarklet:

javascript:(function(){
  location.href = location.href.replace(
    location.hostname, location.hostname + '.ezproxy.its.uu.se'
  );
})()

However, the above first needs you to tell Firefox to load the original URL (so: you'll have to press Return in the location bar) to get the location object populated. Instead, to be prompted for a URL rather than first having your browser (try to) load it:

javascript:(function(){
  var url = prompt('Type URL to browse');
  var suffix = '.ezproxy.its.uu.se';

  /* Don't know how the proxy would handle https or specific ports;
   * let's just copy them...
   * $1 = optional protocol, like 'http[s]://'
   * $2 = domain, like 'superuser.com'
   * $3 = optional port, like ':8080'
   * $4 = rest of the URL, like '/questions/154689/ .. page/154692#154692'
   */
  url = url.replace(
          /(\w*:\/\/)?([^:\/]*)(:[0-9]*)?(.*)/, '$1$2' + suffix + '$3$4'
        );
  if(url.indexOf('http') != 0){
    url = 'http://' + url;
  }
  location.href = url;
})()


And once you've switched to using the proxy, you can use some jQuery magic to rewrite each location in the HTML that is served by the proxy -- but only needed if it doesn't do that for you on the fly. To be saved as a user script (like for Greasemonkey), with some initial code to first ensure jQuery is available, and to only be included for the domain of your proxy server (hence only when you're browsing using that proxy):

// ==UserScript==
// @name           Rewrite URLs to use proxy
// @namespace      http://superuser.com/questions/154689/
// @description    Rewrites absolute URLs to use proxy
// @include        http://*.ezproxy.its.uu.se/*
// ==/UserScript==

var $;
var suffix = '.ezproxy.its.uu.se';

// Rewrites an attribute to include the proxy server address, if a full
// domain is specified in that attribute.
function rewriteAttr(attrName){
  $('[' + attrName + ']').attr(attrName, function(){
    // Don't know how the proxy would handle https or specific ports;
    // let's just copy them...
    // $1 = protocol, like 'http[s]://'
    // $2 = domain, like 'superuser.com'
    // $3 = optional port, like ':8080'
    // $4 = rest of the URL, like '/questions/154689/ .. page/154692#154692'
    return $(this).attr(attrName).replace(
      /(\w*:\/\/)([^:\/]*)(:[0-9]*)?(.*)/, '$1$2' + suffix + '$3$4'
    );
  });
}

// Rewrite anchors such a <a href="http://superuser.com/xyz"> and references
// like <link rel="stylesheet" href="http://sstatic.net/su/all.css">
function letsJQuery() {
  rewriteAttr('href');
  rewriteAttr('src');
}

// Loads jQuery if required. 
// See http://joanpiedra.com/jquery/greasemonkey/
(function(){
  if (typeof unsafeWindow.jQuery == 'undefined') {
    var GM_Head = document.getElementsByTagName('head')[0] 
          || document.documentElement;
    var GM_JQ = document.createElement('script');

    GM_JQ.src = 
      'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
    GM_JQ.type = 'text/javascript';
    GM_JQ.async = true;

    GM_Head.insertBefore(GM_JQ, GM_Head.firstChild);
  }
  GM_wait();
})();

// Check if jQuery's loaded
function GM_wait() {
  if (typeof unsafeWindow.jQuery == 'undefined') {
    window.setTimeout(GM_wait, 100);
  } else {
    $ = unsafeWindow.jQuery.noConflict(true);
    letsJQuery();
  }
}
Arjan
  • 31,511
2

You might also take a look at LibX - "A Browser Plugin for Libraries" that can do this sort of function for you automatically:

Off-campus access via EZProxy or WAM

Support for off-campus access to licensed resources, if your institution uses EZ Proxy or III's WAM. You may reload a page through the proxy, or follow a link via the proxy, making it appear as though you are coming from an on-campus computer. This features gives you access to resources to which only on-campus users have access. You can set up EZProxy such that LibX will automatically detect if a page can be proxied.

1

How about using the URL Parser Firefox Add-on.

Or use the bookmarklet from urlparser.com/bookmarklet.

slhck
  • 235,242
-1

This is the precisely the kind of situation that a Proxy auto-config (PAC) script is intended to solve. The following script will config Firefox so that it will transparently route requests through your local proxy, without having to rewrite them. Save this file somewhere on your filesystem, and then go into the Connection Settings dialog and put the path in the "Automatic proxy configuration URL" setting. (This is supported by all the major browers, not just Firefox.)

function FindProxyForURL(url, host)
{
    return "com.ezproxy.its.uu.se";
}

This is a javascript function, and so conditional logic is possible as well.

Chris Noe
  • 397