如何确定http请求的位置/来源(地址栏)? [英] How to determine the http-request's location/origin (address-bar)?

查看:200
本文介绍了如何确定http请求的位置/来源(地址栏)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,当他尝试加载的页面符合我的条件(正则表达式)时,我正尝试将浏览器重定向到另一个网页.目前看起来像这样:(在此处找到)

So I'm trying to redirect the browser to another webpage when the page he is attempting to load matches my conditions (regex). Currently it looks like this: (came up with it here)

function listener(event) {
    var channel = event.subject.QueryInterface(Ci.nsIHttpChannel);
    var url = event.subject.URI.spec;

    if (isToBeRedirected(url)) {
        // replace url
    }
}

exports.main = function() {
    events.on("http-on-modify-request", listener);
}

但是问题是,这还将替换例如嵌入在页面中的图像的url.我的问题是,是否可以通过键入URL或单击指向页面的链接来更改http请求?因此,每次在地址栏中显示一个URL即可.
我曾想过要阅读当前标签页的网址,然后将其与请求网址进行比较,但是我仍然无法确切地找到答案.

But the problem is, that this will also replace urls to images for example, which are embedded in the page. My question would be, is there a way of varyfying that the http request is made by typing in a url or clicking a link to a page? So simply everytime a url shows up in the address-bar.
I thought about reading the url of the current tab and comparing it to the request url, but I wasnt able to find out exactly how yet.

推荐答案

我有个主意.

如果执行 console.log(Ci.nsIHttpChannel),您会看到它有一堆 flag ,请参阅底部的img.

If you do console.log(Ci.nsIHttpChannel) you see it has a bunch of flags, see img at bottom.

所以我正在考虑通过测试 Ci.nsIHttpChannel.LOAD_INITIAL_DOCUMENT_URI 的标志来测试它是否是主要负载,这意味着它不在子帧中,而是在选项卡的整个文档中.如果它是整个文档,则可能来自链接,URL栏或搜索栏.

So I'm thinking test if it's the main load, meaning its not in a sub frame, but its the whole document of the tab by testing for flag of Ci.nsIHttpChannel.LOAD_INITIAL_DOCUMENT_URI. If its the whole document, its likely from a link, or url bar, or search bar.

所以尝试一下:

function listener(event) {
    var channel = event.subject.QueryInterface(Ci.nsIHttpChannel);
    var url = event.subject.URI.spec;

    if (channel.loadFlags & Ci.nsIHttpChannel.LOAD_INITIAL_DOCUMENT_URI) {
            //the whole document of the tab is changing
            if (isToBeRedirected(url)) {
                // replace url
            }
    }
}

exports.main = function() {
    events.on("http-on-modify-request", listener);
}

我的另一个想法是,是否只希望更改网址栏.比添加事件侦听器更改URL栏的值.更改后,如果用户点击Enter或单击go,则它将注册观察者.然后在测试 Ci.nsIHttpChannel.LOAD_INITIAL_DOCUMENT_URI 之后,取消注册观察者.

My other idea, is if you want only url bar changes. Than add an event listener to change of the url bar value. And after change if user hits enter or clicks go, then it register the observer. Then after testing for Ci.nsIHttpChannel.LOAD_INITIAL_DOCUMENT_URI than unregister the observer.

这篇关于如何确定http请求的位置/来源(地址栏)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆