我使用什么 Firefox 功能来重写和打开活动页面的 URL?

Ego*_*gen 3 firefox url rewrite browser-addons

我们的大学提供代理服务,让我可以像通过大学网络在线一样浏览网站。但是,尽管我很懒惰,但我厌倦了在 Firefox 中转到 URL 编辑字段并将https://superuser.com/更改为http://superuser.com.ezproxy.its.uu.se/并打开它新网址。

相反,我更喜欢在我的Firefox窗口中单击一个按钮。因此,我的问题是,如何创建这样的功能。我对用户脚本、无处不在或附加组件作为解决方案感到满意:我应该使用什么 Firefox 功能来将ezproxy.its.uu.se附加到任何 URL 的域部分,并打开该新 URL?

Arj*_*jan 9

通过将以下内容另存为书签来更改位置:

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

但是,上面首先需要您告诉 Firefox 加载原始 URL(因此:您必须在位置栏中按 Return 键)以填充位置对象。取而代之的是,系统会提示您输入 URL,而不是首先让您的浏览器(尝试)加载它:

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;
})()
Run Code Online (Sandbox Code Playgroud)


一旦您切换到使用代理,您就可以使用一些 jQuery 魔法来重写由代理提供的 HTML 中的每个位置——但只有它不能即时为您执行此操作时才需要。要保存为用户脚本(例如Greasemonkey),使用一些初始代码来首先确保 jQuery 可用,并且仅包含在您的代理服务器域中(因此仅当您使用该代理浏览时):

// ==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();
  }
}
Run Code Online (Sandbox Code Playgroud)