1

デスクトップとインターネット対応テレビの両方で実行される Web アプリケーションがあります。現在、User-Agent ヘッダーを使用して、提供するインターフェイスを決定しています。

私は今、デスクトップ Web サイトへのアクセスをブロックすることなく、デスクトップ ブラウザから TV インターフェイスにアクセスできるようにする任務を負っています。

パッケージアプリ方式

メイン Web サイトをロードする iframe を含むプレーンな html ファイルと、chrome.webRequest.onBeforeSendHeaders 関数を使用して User-Agent をオーバーライドする JavaScript を含むパッケージ化されたアプリを作成しました。

マニフェスト.json

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

index.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 リスナーもトリガーされ、ユーザー エージェントが変更され、サイトへのすべてのリクエストが TV インターフェースにリダイレクトされます。

ホストされたアプリの方法

メイン サイトを指す開始 URL を持つホストされたアプリを作成し、JavaScript を実行して User-Agent をオーバーライドするバックグラウンド ページを作成しました。

マニフェスト.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/*"
    ]
}

index.html

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

問題

  • バックグラウンド ページの JavaScript をインストールすると、常に実行されるため、ユーザーは常に TV サイトにリダイレクトされます。

理想的なソリューションは、アプリが開いているときにのみバックグラウンド ページが実行され、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 に答える