<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>firefox on HAOUTIL.COM</title><link>https://haoutil.com/categories/firefox/</link><description>Recent content in firefox on HAOUTIL.COM</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Tue, 12 Apr 2016 11:26:00 +0800</lastBuildDate><atom:link href="https://haoutil.com/categories/firefox/index.xml" rel="self" type="application/rss+xml"/><item><title>redirector.uc.js UC版Redirector</title><link>https://haoutil.com/posts/redirector-ucjs/</link><pubDate>Tue, 12 Apr 2016 11:26:00 +0800</pubDate><guid>https://haoutil.com/posts/redirector-ucjs/</guid><description>redirector.uc.js是基于Firefox Http请求重定向原理制作的UserChromeJs脚本，对Http请求地址进行重定向，适用于Firefox。
redirector.uc.js支持自定义规则，规则支持正则、通配符以及完全匹配3种类型。具体请参考默认规则：
this.rules = [{ from: &amp;quot;about:haoutil&amp;quot;, // 需要重定向的地址 to: &amp;quot;https://haoutil.googlecode.com&amp;quot;, // 目标地址 wildcard: false, // 可选，true 表示 from 是通配符 regex: false, // 可选，true 表示 from 是正则表达式 resp: false, // 可选，true 表示替换 response body decode: false // 可选，true 表示尝试对 from 解码 },{ from: /^https?:\/\/www\.google\.com\/url\?.*url=([^&amp;amp;]+).*/i, to: &amp;quot;$1&amp;quot;, regex: true },{ from: /^http:\/\/(([^\.]+\.)?google\..+)/i, exclude: /google\.cn/i, // 可选，排除例外规则 to: &amp;quot;https://$1&amp;quot;, regex: true }]; redirector.uc.js已经开源，可以在github上找到它。
redirector_ui.uc.js是规则外置版，提供一个按钮以临时启用和修改规则，规则文件请放到chrome/local文件夹下，下载地址。</description></item><item><title>Firefox Http请求重定向</title><link>https://haoutil.com/posts/firefox-redirect/</link><pubDate>Sat, 31 Jan 2015 16:28:00 +0800</pubDate><guid>https://haoutil.com/posts/firefox-redirect/</guid><description>Firefox有 3个时机 可以重定向Http请求：
1. 在请求创建之前 Firefox支持一种称为内容策略（content policies）的机制[1]，所有资源的加载都需要先经过这个机制的过滤。
扩展和UC脚本可以通过实现nsIContentPolicy接口来注册自己的内容策略，在shouldLoad方法中决定是否拒绝当前Http请求并创建新的请求来达到重定向的目的。
2. 在请求发送到服务器之前 当一个Http请求创建完成时，Firefox会发出一个名为http-on-modify-request的通知。扩展和UC脚本可以通过实现nsIObserver接口来接收这个通知。
在Firefox 20之前，对于关闭一个Http请求并发起新的请求比较麻烦，https-everywhere扩展对此有相关的实现，而在在Firefox 20之后的版本中可以方便的使用redirectTo方法方法来实现。
3. 响应返回后对响应内容进行修改 和Http请求创建一样，在收到响应时，Firefox会发出一个名http-on-examine-response的通知（除了这2种，还有各种各样的通知，详情可以查看MDN）。
修改响应内容可以通过实现nsITraceableChannel接口[2]来实现。
虽然，现在我们在3个时机都可以对Http请求进行重定向，但是还需要解决：
1. 对服务器发起的30X重定向进行重定向 对于30X重定向，上述3个方法都无能为力。好在Firefox提供了nsIChannelEventSink接口，每次30X发生都会调用这个接口。所以通过实现这个接口，就可以对30X后的连接进行判断是否需要重定向了。
2. 带“href=&amp;quot;_blank&amp;quot;的链接不会在新标签页（窗口）打开 这个问题是由于在时机1中，是在新标签（窗口）创建之前就发生了重定向造成的。对于这个问题，我们可以实现nsIDOMEventListener接口，监听DOM点击事件，缓存带“href=&amp;quot;_blank&amp;quot;的链接，在时机1中跳过这些链接即可。</description></item></channel></rss>