编辑:刚刚找到答案:Chrome Extension how to send data from content script to popup.html
我想在网站上计算h2元素,然后在弹出窗口中打印变量。
我实际上可以提醒这个变量,但不能让它传递给弹出窗口。
的manifest.json
{
"manifest_version": 2,
"name": "Getting started example",
"description": "",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab",
"storage",
],
}
popup.html
<!doctype html>
<html>
<head>
</head>
<body>
<span>h2:<span><span id="log"></span>
<script src="popup.js"></script>
</body>
</html>
popup.js
chrome.tabs.executeScript(null, { file: 'content.js' },function(result){
document.getElementById('log').innerHTML = (result);
});
content.js
var result=document.getElementsByTagName("h2").length;