chromeの拡張機能を作成しています
現在、content.jsからJQuaryを使って
ページ内のhtml要素からpタグを取得して改行(<br>)を入れて
文字列としてpopup.htmlに表示させる所で手が止まってしまいました
content.jsにて
コメントアウトしている
text = document.getElementsByTagName('body')[0].outerText;
を用いるとページのbodyにあるテキスト要素が取得されます
JQuaryを使って問題を解決したいのですが
javascriptでも大丈夫です
ご協力よろしくお願いします
以下ソースコードです
json
1{ 2 "name": "thebcom", 3 "manifest_version": 2, 4 "version": "0.0.1", 5 "background":{ 6 "scripts": ["background.js"] 7 }, 8 "content_scripts": [{ 9 "matches": ["<all_urls>"], 10 "js": ["jquary-3.0.0.js","content.js"] 11 }], 12 "browser_action":{ 13 "default_popup":"popup.html" 14 } 15}
popup.js
html
1<html> 2<head> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 4</head> 5 <body style="min-width:300px"> 6 <div id="textView"></div> 7 <script src="popup.js"></script> 8</body> 9</html>
popup.js
javascript
1var textContents = chrome.extension.getBackgroundPage().textContents; 2var div = document.getElementById('textView'); 3div.textContent = textContents;
background.js
javascript
1var textContents = ''; 2chrome.runtime.onMessage.addListener( 3 function (request, sender, sendResponse){ 4 textContents = request.value; 5 } 6);
content.js
javascript
1//var text = 'html'; 2//text += "test"; 3 4$(function() { 5 var text = ''; // テキスト 6 $("p").each(function() { 7 var text_length = $(this).val().length; 8 if(text_length >= 5){ 9 text += $(this).val() + "<br><br>"; 10 } 11 }); 12 }); 13}); 14 15//text = document.getElementsByTagName('body')[0].outerText; 16 17chrome.runtime.sendMessage({ 18 value: text 19}); 20
回答3件
あなたの回答
tips
プレビュー