2017-05-02 2 views
0

Iガイドhere次ています:最新のFirefox WebExtensionでXraysのビジョンを放棄する方法?

私は、IFRAMEのたX線ビジョンを放棄したい:

var foo = $(Components.utils.waiveXrays($("#foobar").get(0).contentWindow.document)); 

上記は次のようにmanifest.jsonを持つコンテンツスクリプトで実行されている:

"content_scripts": [ 
    { 
     "matches": /* something */, 
     "css": ["content.css"], 
     "js": ["jquery-3.2.1.js","content.js"] 
    } 
    ] 

しかし、私は未定義のオブジェクトエラーを取得します:

[firefox/index.js][debug] Firefox stderr: JavaScript error: , line 0: Error: Components.utils is undefined 

Mozillaのウェブサイトのガイドは、古くはだと思います。純粋なWebExtensionの実装ではありません。

正しい最新の方法は、今すぐにする必要がありますか?

答えて

0

調査の数日後(そしてmozilla firefoxメーリングリストで質問)。私はすでに解決策を見つけました。正しいガイドは(2017年5月14日現在)here次のとおりです。

In the SDK, content scripts can share objects with page scripts, using techniques like unsafeWindow and createObjectIn. In WebExtensions, the unsafeWindow is available via wrappedJSObject instead. All the export helper functions are available, too.

私はwindowオブジェクトの放棄するX線のバージョンにアクセスしたい場合は、私が使用する必要があります。

window.wrappedJSObject 

をIFRAMEが内がある場合ページと私はその中のオブジェクトにアクセスしたい。ここでの方法のいずれかです。

document.getElementById("the_iframe").contentWindow.wrappedJSObject 

しかし、私は、IFRAMEがクロスドメインにある場合、wrappedJSObjectは(Firefoxバージョン51.0.1のような)アクセス可能ではないだろうことがわかりました私は次のように別の方法を模索する必要があります。コンテンツでは

"background": { 
    "scripts": ["background.js"] 
    }, 

    "content_scripts": [ 
    { 
     "matches": ["*://*.something"], 
     "css": ["content.css"], 
     "all_frames": true, /* ADD THIS LINE */ 
     "js": ["jquery-3.2.1.js","content.js"] 
    } 
    ] 

は、子はiframeとトップページの間messaging bridgeを提供し、バックグラウンドスクリプトを実装し、すべての子はiframeを注入コンテンツスクリプトを提供し、 .js、そのような何か:background.jsで

if(window.top == window.self) { // main 
    main(); 
} 
else if(window.name == "frameA") { // match frameA 
  browser.runtime.onMessage.addListener(function(msg) { 
    /* Check the message msg and then access the waived X rays vision by window.wrappedJSObject */ 
    if(msg matches something) { 
     var ret = window.wrappedJSObject.foobar(msg.data); 
     /* Send back the return value to background.js by browser.runtime.sendMessage */ 
     browser.runtime.sendMessage({"returnVal": ret, "from": "frameA"}); 
    } 
    } 
    }); 
} 

を、いくつかのメッセージの転送を行います。

function forward_to_all(r) 
{ 
    var forwardMessage = function(tabs) { 
    for(let tab of tabs) { 
     browser.tabs.sendMessage(tab.id,r); 
    } 
    } 
    browser.tabs.query({currentWindow:true, active:true}).then(forwardMessage); 
} 
browser.runtime.onMessage.addListener(function(msg) { 
    if(msg matches something) { 
    forward_to_all(msg); 
    } 
}); 
1

firefox webextensionsでxraysを放棄することはできません。できるだけ早く無効にすることができるバグです。

I think the guide on Mozilla website is outdated.

実際。

What should be the correct latest method now?

あなたがしたいこと(つまり、xrayラッパーを放棄する)はできません。おそらく、あなたが望むものを達成するための新しい方法について考えてみるのはおそらく一番です(その説明ではわかりません)。

関連する問題