2011-09-11 5 views
1

ウェブページ内のiframeの内容を取得するための要件が​​あります。私は拡張のためのボタンをクリックしたときに、これは正しく---javascript関数にマイナーチェンジが必要

<html> 
<head> 

</head> 
<body> 
<script type="text/javascript"> <!-- Function can also be loaded from an external file --> 
function getContentFromIframe(iFrameName) // the function has one parameter: the iframe's ID. 
{           // I did it this way, so you can call it for multiple iframes. 

    var myIFrame = document.getElementById(iFrameName); // Creating an object of the iframe 
var content = myIFrame.contentWindow.document.body.innerHTML; // Getting it's content into a variable 

// Basically now, in the variable 'content' you have the content of your iframe, 
// and can do anything you want with it. 
alert('content: ' + content); // here it is 


// You can even choose to change it afterwards 
content = 'The inside of my frame has now changed'; // create a new content 
myIFrame.contentWindow.document.body.innerHTML = content; // and set it 
} 

</script> 
<iframe id="iframe1" src="http://www.google.com/"></iframe> <!-- Instantiating the iframe --> 
<br /> 
<a href="#" onclick="getContentFromIframe('iframe1')">Get the content</a> <!-- Calling the function --> 

</body> 

</html> 

は今、私はGoogle Chromeで上記のコードを使用する予定のない次のコードは、その後javascriptのコードが内に噴射される基本的EXTENSION-ましたウェブページ、ここではiframe1という名前のiframeの内容を取得する必要があります。

私は関数 'getContentFromIframe'でiframeのidの値が入力パラメータとして渡されることを理解しています。だから、私は、IFRAMEのコンテンツの任意の値を取得していない午前しかしiframe--

var myIFrame = document.getElementById('iframe1'); // Creating an object of the iframe 
var content = myIFrame.contentWindow.document.body.innerHTML; // Getting it's content into a variable 

// Basically now, in the variable 'content' you have the content of your iframe, 
// and can do anything you want with it. 
alert('content: ' + content); // here it is 

のコンテンツを取得することを望んで、次のようにコードを変更しました。私はここで間違っていますか?私は多くのJavaScriptを知らないので、私の構文がどこかで間違っていると思われますか?下記の

{ 
    "name": "Link Submitter", 
    "version": "1.0", 
    "background_page": "bg.html", 
    "description": "Link Submitter by SEO Power Toys", 
    "browser_action": { 
     "name": "Send Data", 
     "default_icon": "icon.png", 
     "default_title": "Send data to Link Submitter"  // optional; shown in tooltip 
    }, 
    "permissions": [ "tabs", 
     "bookmarks", 
      "http://*/*", 
      "https://*/*", 
      "unlimitedStorage" 
    ] 

} 

はまた、下記の---私のmanifest.jsonをの内容である私はGoogle ChromeのEXTENSION-

を実行しようとすると、私は、JavaScriptコンソールに取得していますエラーメッセージです
Uncaught TypeError: Cannot read property 'document' of undefined 
+2

[同一起源ポリシー](http://en.wikipedia.org/wiki/Same_origin_policy)に違反しようとしている必要があります。 – Shef

+0

あなたの "manifest.json"には何がありますか?あなたはおそらく、適切な権限を持っていないでしょう。 –

+0

また、@ Shref注記のとおり、これは同じ起源の違反です。それを回避するにはいくつかの方法があります...しかし、それはセキュリティに賢明ではありません。 –

答えて

1

のコンテンツに直接アクセスしたり操作したりすることはできません。自分とは異なるドメインのコンテンツを含む要素。フレーム間で通信するにはいくつかの方法がありますが、両方のドメインのコンテンツを準備する必要があります。これらの(かなり新しい)APIでランダムなページが動作することは期待できません。

今や、の内線番号から、もちろん、ルールが異なります。ただし、通常のWebページでコードを操作している場合は、機能しません。

Hereは、コードのテストケースです。それはうまくいくようです。

+0

上記の私のコメントを参照してください – user893664

+0

同様の実験を試してみましょう...私はあなたが "--allow-file-from-files-from-files"スイッチについて話していると推測します。 – Pointy

+0

こんにちは、http://stackoverflow.com/questions/3102819/chrome-disable-same-origin-policyを参照してください... – user893664

関連する問題