2012-06-14 41 views
1

我們有一個在桌面和啓用了互聯網的電視上運行的Web應用程序。目前我們使用User-Agent頭來確定要提供哪個接口。Chrome應用程序與在受限範圍內運行的後臺腳本

我現在的任務是讓電視界面可以從桌面瀏覽器訪問而不會阻止對桌面網站的訪問,所以我想包裝它作爲一個鉻應用程序會做的伎倆,它的確如此,但有幾個問題。

封裝應用方法

我創建了一個打包應用程序包含加載主站,並使用chrome.webRequest.onBeforeSendHeaders功能覆蓋用戶代理一些JavaScript的iframe一個純HTML文件。

的manifest.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偵聽器,當導航到另一個選項卡中的主站點時,導致User-Agent成爲修改並且對網站的所有請求都被重定向到電視界面。

託管的應用方法

我創建的起始URL指向主要網站託管應用程序,創建運行的JavaScript覆蓋用戶代理後臺頁面。

的manifest.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始終運行,因此用戶總是重定向到電視現場

理想的解決方案是託管應用程序,該應用程序僅在應用程序打開時運行後臺頁面,而onBeforeSendHeaders偵聽器不會影響Chrome應用程序之外的任何請求。

有沒有人有任何想法我可以做到這一點?

回答

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:/^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))[email protected])?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))[email protected])?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))[email protected])?([^:\/?#]*)(?::(\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() function stolen from PHP.js