2012-07-24 63 views
5

私はこのような構造でページを変更するためにクロムuserscriptまたはTampermonkeyスクリプトを使用しようとしている:iframeのjavascriptにusercriptからアクセスするにはどうすればよいですか?

<body> 
content up here 

<iframe id="main" src="foo.dat"></iframe> 
</body> 

IFRAMEは同一生成元です。

iframe#mainにある関数にアクセスする必要があります。私はと思った私はそれを得るためにunsafeWindowを使うことができましたが、私は何も得ていないか、またはundefinedが返されました。

私は物事のスルーを試してみた:

  • iframeの新しいスクリプト要素を作成しようとしたが、それも未定義$('frame#main').contents().append(script)または$('frame#main').contents()[0].createElement('script')

  • window.frames["#main"].contentWindow戻って親に接続します。

は私が現時点で思い出すことができない他の多くのものを試してみましたが、私はすべての私の考えを使い果たし、私は数える何よりもごみを入力してると感じています。
iFrameのunsafeWindowで遊ぶ方法を理解できません。

+0

これはすべて同じ起源です。 – Skinner927

答えて

10
  1. unsafeWindow Chrome、Tampermonkey、またはFirefoxのフレーム/ iframeでうまく再生されません。
  2. jQueryを使用してグローバルな(フレームへの)JSへのアクセスを試みても機能しません。
  3. userscriptsは、@include,@exclude、および/または@matchの要件を満たすiframeで実行されます。

したがって、複数のスクリプトの実行を考慮する必要があります。次に、達成しようとしていることに応じて2つの基本的な方法があります。あなたは

(A)this answerのように、特定のフレームにスクリプトを合わせることができます。

または(B)JSを注入し、特別なframesオブジェクトを使用して、必要な特定の機能を取得します。

次のスクリプトは、両方を示しています。 Tampermonkey (またはFirefoxのGreasemonkey)にインストールし、this test page at jsBinにアクセスしてください。

// ==UserScript== 
// @name  _Calling iframe functions 
// @namespace _pc 
// @include  http://jsbin.com/ugoruz/* 
// @include  http://jsbin.com/okequw/* 
// ==/UserScript== 

console.log ("Script start..."); 

/*--- This next function call will work in Firefox or Tampermonkey ONLY, 
    not pure Chrome userscript. 
*/ 
console.log ("calling functionOfInterest()..."); 
unsafeWindow.functionOfInterest(); 


if (window.top === window.self) { 
    //--- Code to run when page is the main site... 
    console.log ("Userscript is in the MAIN page."); 

    //--- The frames object does not play nice with unsafeWindow. 
    /*--- These next three work in Firefox, but not Tampermonkey, nor pure Chrome. 
    console.log ("1", frames[1].variableOfInterest);    // undefined 
    console.log ("2", unsafeWindow.frames[1].variableOfInterest); // undefined 
    console.log ("3", frames[1].unsafeWindow);      // undefined 
    */ 
    /*--- This next would cause a silent crash, all browsers... 
    console.log ("4", unsafeWindow.frames[1].unsafeWindow.variableOfInterest); 
    */ 

    //--- To get at iFramed JS, we must inject our JS. 
    withPages_jQuery (demoAccessToFramedJS); 
} 
else { 
    //--- Code to run when page is in an iframe... 
    console.log ("Userscript is in the FRAMED page."); 
    console.log ("The frame's ID is:", window.self.frameElement.id); 
} 


function demoAccessToFramedJS ($) { 
    $("body").prepend (
      '<button id="gmMain">Run JS on main window</button>' 
     + '<button id="gmFrame">Run JS on iframe</button>' 
    ); 

    $("#gmMain, #gmFrame").click (function() { 
     if (this.id === "gmMain") { 
      functionOfInterest(); 
     } 
     else { 
      frames[1].functionOfInterest(); 
     } 
     console.log (this.id + "was clicked."); 
    }); 
} 

function withPages_jQuery (NAMED_FunctionToRun) { 
    //--- Use named functions for clarity and debugging... 
    var funcText  = NAMED_FunctionToRun.toString(); 
    var funcName  = funcText.replace (/^function\s+(\w+)\s*\((.|\n|\r)+$/, "$1"); 
    var script   = document.createElement ("script"); 
    script.textContent = funcText + "\n\n"; 
    script.textContent += 'jQuery(document).ready(function() {'+funcName+'(jQuery);});'; 
    document.body.appendChild (script); 
}; 

console.log ("Script end"); 



あなたはスクリプトがメインページの両方からとiframeから関数を実行していることがわかります。コンソール出力(Tampermonkey)は次のようになります。

 
Tampermonkey started 
Script start... 
calling functionOfInterest()... 
Userscript is in the MAIN page. 
Script end 
Tampermonkey started 
Script start... 
calling functionOfInterest()... 
Userscript is in the FRAMED page. 
The frame's ID is: iframe2 
Script end 

あなたはunsafeWindow行(複数可)を削除する場合それはまたまっすぐアップクロームuserscriptとして動作します。

+0

あなたが置いたものは私のためにはうまくいきませんでしたが、これはあなたの参照された答えと組み合わされました。基本的に、私がやったことは、あなたが示唆したように、両方のフレームにスクリプトを含めることでしたが、スクリプトのロード先に応じてスクリプトの一部を実行するだけでした。控えめな回避策ですが、この時点では何かを取っていきます。 ありがとうございます。 – Skinner927

関連する問題