2012-07-19 69 views
2

匹配這個答案指定說明如何訪問所有I幀的內容上gmail.com https://stackoverflow.com/a/9439525/222236Chrome擴展:不安全的JavaScript嘗試與URL域名來訪問框架,協議和端口必須

但在mail.google.com它引發此錯誤:

Unsafe JavaScript attempt to access frame with URL https://plus.google.com/u/0/_/... from frame with URL https://mail.google.com/mail/u/0/#inbox. Domains, protocols and ports must match. 

我嘗試添加*://plus.google.com/*到擴展的艙單的比賽,但它並沒有幫助。

更新:檢查用於訪問內容的作品前的網址,但我的邏輯是非常粗略的時刻,因爲它僅檢查谷歌加:

 if(-1==iframes[i].src.indexOf('plus.google.com')) { 
      contentDocument = iframes[i].contentDocument; 
      if (contentDocument && !contentDocument.rweventsadded73212312) { 
       // add poller to the new iframe 
       checkForNewIframe(iframes[i].contentDocument); 
      } 
     } 
+0

我相信在許多情況下,它可能是遍歷所有子幀的要求,並且從控制流中排除外部域會破壞應用程序的主要功能。所以,在擴展chrome的情況下,我建議用清單中的_content_scripts_「all_frames」:true'聲明替換循環,並使用可選邏輯來合併結果(如果需要)。 – Stan 2015-11-26 14:15:28

回答

2

訪問被阻止,因爲same origin policy
避免錯誤的正確方法是從不同來源中排除幀。你的邏輯確實很粗糙。它沒有專門查看主機名,也沒有考慮其他域。
反轉邏輯具有健壯的解決方案:

if (iframes[i].src.indexOf(location.protocol + '//' + location.host) == 0 || 
    iframes[i].src.indexOf('about:blank') == 0 || iframes[i].src == '') { 

白名單的說明:

  • protocol://host/ = https://mail.google.com
    顯然,當前主機必須被允許
  • about:blank和一個空字符串被動態創建和通過的GMail腳本
    這些幀。
1

mail.google.complus.google.com是不一樣的領域。現代Web瀏覽器中的JavaScript實現不允許跨域腳本。

沒有訴諸不同種類的hackery,解決這個問題的正確方法是通過CORS(http://en.wikipedia.org/wiki/Cross-origin_resource_sharing),在這種情況下不可用。