Web ページからメタデータといくつかのテキスト要素を抽出する chrome 拡張機能に取り組んでいます。その情報が特定のページのフレーム内に含まれていると、問題が発生します。
私が作業しているページの次のフレームセットの例を見てください (実際のフレーム名、タイトル、ソースは簡潔にするために変更されています)。
<frameset id="fs" border="0" frameborder="no">
<frame src="/src1" name="frame1" noresize="noresize" scrolling="no" frameborder="0" title="FrameNumber1">
<frame src="/src2" name="frame2" noresize="noresize" scrolling="no" frameborder="0" title="FrameNumber2">
<frame src="/src3" name="frame3" noresize="noresize" scrolling="auto" frameborder="0" title="FrameNumber3">
<frame src="/src4" name="frame4" noresize="noresize" scrolling="no" title="FrameNumber4">
</frameset>
そして、FrameNumber3 の次の HTML スニペットを検討してください:
<html>
<head>
<title>FrameNumber3</title>
</head>
<body>
<h1 id="heading">This is what I want</h1>
</body>
</html>
私はさまざまな方法を試しましたが、それらのほとんどは、jquery セレクターの適切なコンテキスト パラメーターでのさまざまな推測を中心に展開しています。
数十を試しましたが、実際に機能すると思われるものの例を次に示します。
alert($('#heading', window.parent.frames[0].document).text());
//Uncaught TypeError: Cannot read property 'document' of undefined
alert($('#heading', window.parent.frames[2].document).text());
//Tried this, because I want the 3rd frame's information
//Uncaught TypeError: Cannot read property 'document' of undefined
フレームへのjQuery参照を使用してフレーム内の要素にアクセスする
alert($('#heading', $('frame').get(2).contentWindow.document).text());
//Uncaught TypeError: Cannot read property 'document' of undefined
他のページ経由:
alert($('#heading', $('frame[title="FrameNumber3"]').document).text());
//no error but returns "undefined"
alert($('#heading', $('frame[title="FrameNumber3"]').contents()).text());
//no error but returned "undefined"
私はそれを処理したので、Chrome 拡張機能内のアクセス許可の問題ではないと思います。フレームDOMに到達することについて、私が理解していないことがある可能性が高いと思います。また、フレームに関してはjqueryが最も信頼できるものではないことも読んだので、それも問題になる可能性があると思います。
フィードバックや支援をお寄せいただきありがとうございます。