在 Firefox 中加载页面之前修改 URL

发布于 2024-10-20 05:18:04 字数 138 浏览 4 评论 0原文

我想为与我的模式匹配的 URL 添加前缀。当我在 Firefox 中打开新选项卡并输入匹配的 URL 时,页面不应正常加载,应首先修改 URL,然后开始加载页面。

是否可以在页面开始加载之前通过 Mozilla Firefox 插件修改 URL?

I want to prefix URLs which match my patterns. When I open a new tab in Firefox and enter a matching URL the page should not be loaded normally, the URL should first be modified and then loading the page should start.

Is it possible to modify an URL through a Mozilla Firefox Addon before the page starts loading?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南烟 2024-10-27 05:18:04

浏览 HTTPS Everywhere 插件建议执行以下步骤:

  1. 注册 观察者 用于“http-on-modify-request” 观察者主题,具有 nsIObserverService
  2. 如果观察者通知的主题是 nsIHttpChannelsubject 的实例,请继续。 URI.spec(URL)符合您的条件
  3. 创建一个新的 nsIStandardURL
  4. 创建新的 nsIHttpChannel
  5. 替换旧通道。在 HTTPS Everywhere 中执行此操作的代码非常密集,可能比您需要的要多得多。我建议从 开始chrome/content/IOUtils.js

请注意,您应该为整个应用程序注册一个“http-on-modify-request”观察器,这意味着您应该将其放在 XPCOM 组件中(有关示例,请参阅 HTTPS Everywhere)。

以下文章不能直接解决您的问题,但它们确实包含许多可能对您有用的示例代码:

Browsing the HTTPS Everywhere add-on suggests the following steps:

  1. Register an observer for the "http-on-modify-request" observer topic with nsIObserverService
  2. Proceed if the subject of your observer notification is an instance of nsIHttpChannel and subject.URI.spec (the URL) matches your criteria
  3. Create a new nsIStandardURL
  4. Create a new nsIHttpChannel
  5. Replace the old channel with the new. The code for doing this in HTTPS Everywhere is quite dense and probably much more than you need. I'd suggest starting with chrome/content/IOUtils.js.

Note that you should register a single "http-on-modify-request" observer for your entire application, which means you should put it in an XPCOM component (see HTTPS Everywhere for an example).

The following articles do not solve your problem directly, but they do contain a lot of sample code that you might find helpful:

茶花眉 2024-10-27 05:18:04

感谢 Iwburk,我才能够做到这一点。

我们可以用一个新的覆盖 nsiHttpChannel 来实现这一点,这样做有点复杂,但幸运的是,附加组件 https-everywhere 实现了这一点以强制建立 https 连接。

https-everywhere 的源代码可用 这里

所需的大部分代码都在文件中

IO Util.js
ChannelReplacement.js

只要我们设置了 Cc、Ci 等基本变量和函数 xpcom_generateQI 已定义。

var httpRequestObserver =
{ 
  observe: function(subject, topic, data) {
    if (topic == "http-on-modify-request") {

        var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);     
        var requestURL = subject.URI.spec;

        if(isToBeReplaced(requestURL))  {

            var newURL = getURL(requestURL);        
             ChannelReplacement.runWhenPending(subject, function() {
                    var cr = new ChannelReplacement(subject, ch);
                    cr.replace(true,null);
                    cr.open();
                });
        }
    }

  },

  get observerService() {
    return Components.classes["@mozilla.org/observer-service;1"]
                     .getService(Components.interfaces.nsIObserverService);
  },

  register: function() {
    this.observerService.addObserver(this, "http-on-modify-request", false);

  },

  unregister: function() {
    this.observerService.removeObserver(this, "http-on-modify-request");

  }
};


httpRequestObserver.register();

该代码将替换请求而不是重定向。

虽然我已经很好地测试了上面的代码,但我不确定它的实现。据我所知,它复制了所请求通道的所有属性并将它们设置为要覆盖的通道。之后,以某种方式使用新通道提供原始请求所请求的输出。

PS 我看过一篇 SO 帖子,其中建议了这种方法。

Thanks to Iwburk, I have been able to do this.

We can do this my overriding the nsiHttpChannel with a new one, doing this is slightly complicated but luckily the add-on https-everywhere implements this to force a https connection.

https-everywhere's source code is available here

Most of the code needed for this is in the files

IO Util.js
ChannelReplacement.js

We can work with the above files alone provided we have the basic variables like Cc,Ci set up and the function xpcom_generateQI defined.

var httpRequestObserver =
{ 
  observe: function(subject, topic, data) {
    if (topic == "http-on-modify-request") {

        var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);     
        var requestURL = subject.URI.spec;

        if(isToBeReplaced(requestURL))  {

            var newURL = getURL(requestURL);        
             ChannelReplacement.runWhenPending(subject, function() {
                    var cr = new ChannelReplacement(subject, ch);
                    cr.replace(true,null);
                    cr.open();
                });
        }
    }

  },

  get observerService() {
    return Components.classes["@mozilla.org/observer-service;1"]
                     .getService(Components.interfaces.nsIObserverService);
  },

  register: function() {
    this.observerService.addObserver(this, "http-on-modify-request", false);

  },

  unregister: function() {
    this.observerService.removeObserver(this, "http-on-modify-request");

  }
};


httpRequestObserver.register();

The code will replace the request not redirect.

While I have tested the above code well enough, I am not sure about its implementation. As far I can make out, it copies all the attributes of the requested channel and sets them to the channel to be overridden. After which somehow the output requested by original request is supplied using the new channel.

P.S. I had seen a SO post in which this approach was suggested.

擦肩而过的背影 2024-10-27 05:18:04

您可以监听 页面加载事件 或者 DOMContentLoaded事件代替。或者你可以制作一个 nsIURIContentListener 但这是可能更复杂。

You could listen for the page load event or maybe the DOMContentLoaded event instead. Or you can make an nsIURIContentListener but that's probably more complicated.

咆哮 2024-10-27 05:18:04

是否可以在页面开始加载之前通过 Mozilla Firefox 插件修改 URL?

是的,这是可能的。

通过设置 contentScriptWhen: "start" 使用 Addon-SDK 的 page-mod

然后在完全阻止文档被解析后,您可以

  1. 从同一域获取不同的文档并将其注入页面中。
  2. 经过一些 document.URL 处理后,执行 location.replace() 调用

以下是执行 1 的示例。 https://stackoverflow.com/a/36097573/6085033

Is it possible to modify an URL through a Mozilla Firefox Addon before the page starts loading?

YES it is possible.

Use page-mod of the Addon-SDK by setting contentScriptWhen: "start"

Then after completely preventing the document from getting parsed you can either

  1. fetch a different document from the same domain and inject it in the page.
  2. after some document.URL processing do a location.replace() call

Here is an example of doing 1. https://stackoverflow.com/a/36097573/6085033

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文