1

我们有一个在桌面和支持互联网的电视上运行的网络应用程序。目前我们正在使用 User-Agent 标头来确定要服务的接口。

我现在的任务是在不阻止访问桌面网站的情况下从桌面浏览器访问电视界面,因此我认为将其包装为 chrome 应用程序可以解决问题,但存在一些问题。

打包应用方法

我使用 chrome.webRequest.onBeforeSendHeaders 函数创建了一个打包的应用程序,其中包含一个加载主网站的 iframe 和一些 javascript 以覆盖用户代理的纯 html 文件。

清单.json

{
    "app": {
        "launch": {
            "local_path": "index.html"
        }
    },
    "permissions": [
        "webRequest", "webRequestBlocking", "cookies",
        "*://my.site.url/*"
    ]
}

索引.html

<html>
    <head>
        <script src="main.js"></script>
    </head>
    <body>
        <iframe src="http://my.site.url"></iframe>
    </body>
</html>

main.js

chrome.webRequest.onBeforeSendHeaders.addListener(
    function(details) 
    {
        var headers = details.requestHeaders;

        for (var i = 0; i < headers.length; ++i) 
        {
            if (headers[i].name === 'User-Agent') 
            {
                headers[i].value += " Chromebox"
                break;
    }
        }
        return {requestHeaders: headers};
    }, 
    {
        urls: ["*://my.site.url/*"]
    }, 
    ["blocking", "requestHeaders"]
);

问题

  • 我必须允许将整个站点加载到 iframe 中,从而将其打开以进行可能的点击劫持
  • 当应用程序打开时,它还会在导航到另一个选项卡中的主站点时触发 onBeforeSendHeaders 侦听器,从而导致用户代理被修改,并且对站点的所有请求都被重定向到电视界面。

托管应用程序方法

我创建了一个托管应用程序,其起始 url 指向主站点,并创建了一个运行 javascript 以覆盖用户代理的后台页面。

清单.json

{
    "app": {
        "launch": {
            "web_url": "http://my.site.url"
        }
    },
    "background_page": "https://my.site.url/chromebox/index.html",
    "permissions": [
        "webRequest", "webRequestBlocking", "background",
        "*://my.site.url/*"
    ]
}

索引.html

<html><script src="main.js"></script></html>

问题

  • 安装后,后台页面 javascript 始终运行,因此用户始终被重定向到电视网站

理想的解决方案是托管应用程序,只有在应用程序打开时才运行后台页面,并且 onBeforeSendHeaders 侦听器不会影响 chrome 应用程序之外的任何请求。

有人知道我如何实现这一目标吗?

4

1 回答 1

0

我认为我没有直接解决您的问题,但如果我这样做了,这是main.js您的托管应用程序方法:

function parse_url(str,component){var key=['source','scheme','authority','userInfo','user','pass','host','port','relative','path','directory','file','query','fragment'],ini=(this.php_js&&this.php_js.ini)||{},mode=(ini['phpjs.parse_url.mode']&&ini['phpjs.parse_url.mode'].local_value)||'php',parser={php:/^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/};var m=parser[mode].exec(str),uri={},i=14;while(i--){if(m[i]){uri[key[i]]=m[i];}}
if(component){return uri[component.replace('PHP_URL_','').toLowerCase()];}
if(mode!=='php'){var name=(ini['phpjs.parse_url.queryKey']&&ini['phpjs.parse_url.queryKey'].local_value)||'queryKey';parser=/(?:^|&)([^&=]*)=?([^&]*)/g;uri[name]={};uri[key[12]].replace(parser,function($0,$1,$2){if($1){uri[name][$1]=$2;}});}
delete uri.source;return uri;}

chrome.webRequest.onBeforeSendHeaders.addListener(function(details){
    if(!details.requestHeaders) return details; // skip parsing if missing request headers
    var domain = parse_url(details.url,'host');
    if(host.indexOf('my.site.com')!==0) return details; // skip parsing if it doesn't go to your site
    host=host.split('/'); if(host[1] && host[1]!=='') return details; // skip parsing if it doesn't go to the index

    var headers = details.requestHeaders;
    for(var i in headers){
        if(!headers[i] || !headers[i].name || !headers[i].value) continue;
        if(headers[i].name!=='string') continue;
        if(headers[i].name.toLowerCase()!=='user-agent') continue;

        details.requestHeaders[i].value += ' Chromebox'
        break;
    }

    return details; //  OK BOSS
});

Parse_url()从 PHP.js 窃取的函数

于 2012-06-15T06:53:47.180 回答