What Firefox functionality do I use to rewrite and open the URL of the active page?

3

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?

Egon Willighagen

Posted 2010-06-20T12:11:14.610

Reputation: 457

Just rewriting that one to the other is not enough fun. So, please tell us what the URL would be for pages like this very question? – Arjan – 2010-06-20T12:22:47.593

Just like the example given, it only requires changing the domain, so the URL to this question would be: http://superuser.com.ezproxy.its.uu.se/questions/154689/how-can-i-add-a-button-to-firefox-that-will-rewrite-and-open-the-url-of-the-a

– Egon Willighagen – 2010-06-20T12:34:22.503

Answers

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.

Peter Murray

Posted 2010-06-20T12:11:14.610

Reputation: 827

Nice indeed! (@Egon, you can easily change the accepted answer if you want to!) – Arjan – 2010-06-20T20:02:30.467

Thanks for adding the blockquote with the feature description, Arjan! – Peter Murray – 2010-06-20T20:57:53.150

While I really found the JavaScript solution quite interesting, this indeed seems to be a better solution. – Egon Willighagen – 2010-06-26T07:48:35.553

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

Posted 2010-06-20T12:11:14.610

Reputation: 29 084

1I'm lazy, I know. How do I create and save a bookmarklet? – Egon Willighagen – 2010-06-20T13:11:35.530

OK, figured that out: Bookmarks -> Organize Bookmarks; then select Bookmarks Toolbar and right click on the list of items and select New. Then I just copy/paste the above code as URL. – Egon Willighagen – 2010-06-20T14:19:53.687

@Egon, ok! For future reference I also added a link to "saving the following as a bookmarklet", and added some more rewriting just in case the proxy wouldn't do that for you -- but I guess it does this on the fly.

– Arjan – 2010-06-20T14:25:22.273

@Egon, I just realized that the bookmarklet might not work for URLs that your browser has not yet loaded? I've added another option... :-) – Arjan – 2010-06-20T15:06:21.733

Thanx for all the detail. The proxy indeed rewrites everything nicely. And my practical use case is scientific papers... you typically end up on a journal homepage with the abstract in front of you, but the PDF would not be downloadable from outside the university network... so, the URL is typically already loaded, and your first solution is working fine! Thanx! – Egon Willighagen – 2010-06-20T15:13:28.833

0

How about using the URL Parser Firefox Add-on.

Or use the bookmarklet from urlparser.com/bookmarklet.

URLParser.com

Posted 2010-06-20T12:11:14.610

Reputation: 1

-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

Posted 2010-06-20T12:11:14.610

Reputation: 377

I think you missed the point that this is not a proxy server you use by setting the browser to connect through that proxy, but a proxy that you use by changing the URL. – njsg – 2013-02-17T19:04:39.670