現在のページを読み取り、そこから特定の html/xml タグを検出する chrome 拡張機能を構築しています。
たとえば、現在のページに次のタグまたはデータが含まれている場合:
some random text here and there
<investmentAccount acctType="individual" uniqueId="1629529524">
<accountName>state bank of america</accountName>
<accountHolder>rahul raina</accountHolder>
<balance balType="totalBalance">
<curAmt curCode="USD">516545.84</curAmt>
</balance>
<asOf localFormat="MMM dd, yyyy">2013-08-31T00:00:00</asOf>
<holdingList>
<holding holdingType="mutualFund" uniqueId="-2044388005">
<description>Active Global Equities</description>
<value curCode="USD">159436.01</value>
</holding>
<holding holdingType="mutualFund" uniqueId="-556870249">
<description>Passive Non-US Equities</description>
<value curCode="USD">72469.76</value>
</holding>
</holdingList>
<transactionList/>
</investmentAccount>
</site>
some data 123
<site name="McKinsey401k">
<investmentAccount acctType="individual" uniqueId="1629529524">
<accountName>rahuk</accountName>
<accountHolder>rahuk</accountHolder>
<balance balType="totalBalance">
<curAmt curCode="USD">516545.84</curAmt>
</balance>
<asOf localFormat="MMM dd, yyyy">2013-08-31T00:00:00</asOf>
<holdingList>
<holding holdingType="mutualFund" uniqueId="1285447255">
<description>Special Sits. Aggr. Long-Term</description>
<value curCode="USD">101944.69</value>
</holding>
<holding holdingType="mutualFund" uniqueId="1721876694">
<description>Special Situations Moderate $</description>
<value curCode="USD">49444.98</value>
</holding>
</holdingList>
<transactionList/>
</investmentAccount>
</site>
したがって、タグを識別して、開始タグと終了タグの間のテキストを出力する必要があります。つまり、「State bank of america」と「rahukk」です。
だから、これは私が今までやったことです:
function countString(document_r,a,b) {
var test = document_r.body;
var text = typeof test.textContent == 'string'? test.textContent : test.innerText;
var testRE = text.match(a+"(.*)"+b);
return testRE[1];
}
chrome.extension.sendMessage({
action: "getSource",
source: "XML DETAILS>>>>>"+"\nAccount name is: " +countString(document,'<accountName>','</accountName>')
});
ただし、これは、ページで最初に検出されたタグ、つまり「State bank of america」の内部テキストのみを出力します。
ページの最後のタグの内部テキストである「rahukk」のみ、またはその両方を印刷したい場合はどうすればよいでしょうか。
ページ内で最後に見つかったタグの内部テキストを印刷するにはどうすればよいですか、またはすべてのタグをどのように印刷しますか?
前もって感謝します。
編集:それ自体の上のドキュメントは、ページのコンテンツを配置したばかりのHTMLページです
更新:だから私は以下の提案からあちこちでいくつかを行い、このコードで到達できる最高のものを行いました:
function countString(document_r) {
var test = document_r.body;
var text = test.innerText;
var tag = "accountName";
var regex = "<" + tag + ">(.*?)<\/" + tag + ">";
var regexg = new RegExp(regex,"g");
var testRE = text.match(regexg);
return testRE;
}
chrome.extension.sendMessage({
action: "getSource",
source: "XML DETAILS>>>>>"+"\nAccount name is: " +countString(document)
});
しかし、これは私に与えました:
XML DETAILS>>>>> 退職金制度 (利益分配型退職金制度 (PSRP) および金銭購入年金制度 (MPPP)),退職金制度 (利益分配型退職金制度 (PSRP) および金銭購入年金制度 (MPPP)),退職プログラム (利益分配型退職金制度 (PSRP) およびマネー パーチェス ペンション プラン (MPPP))
これも同じ XML がページに 3 回存在したためです。私が望むのは、その正規表現が最後の XML からのみ一致することであり、タグ名も必要ありません。
したがって、私の望ましい出力は次のようになります。
XML DETAILS>>>>> 退職金制度 (利益分配型退職金制度 (PSRP) および金銭購入年金制度 (MPPP))