我正在使用两个按钮进行扩展:开始和停止。点击开始按钮的HTML页面,应该开始刷新特定的计时器后,并在点击停止它应该停止刷新。Content.js脚本从不调用表格行和刷新页面
在每次刷新后,HTML页面中都有一个表格,用id myTable.So表示,我想要这个表格的行数。
为了得到这个我没有这样的事:
首先用于弹出,我做了popup.js
window.onload = function() {
document.getElementById("startbutton").onclick = function() {
chrome.extension.sendMessage({
type: "table-row-count_start"
});
}
document.getElementById("stopbutton").onclick = function() {
chrome.extension.sendMessage({
type: "table-row-count_stop"
});
}
}
在background.js
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
switch(request.type) {
case "table-row-count_start":
alert("Refershing started");
RefreshAndCount();
break;
case "table-row-count_stop":
alert("Stop Refershing");
break;
}
return true;
});
var RefreshAndCount = function(){
chrome.tabs.getSelected(null, function(tab){
chrome.tabs.sendMessage(tab.id, {type: "table-row-count"});
chrome.browserAction.setBadgeText({text: "Counting!"});
});
}
这将使呼叫content.js,因为我们需要与HTML页面的DOM进行交互。在这个脚本中,我做了这样的事情:
chrome.extension.onMessage.addListener(function(message, sender, sendResponse) {
switch(message.type) {
case "table-row-count":
var x = document.getElementById("myTable").rows.length;
alert("Row count = " + x);
var Refresh = confirm("Do you want to refresh the page?");
if (Refresh)
{
setTimeout("location.reload(true);",5000);
}
break;
}
});
脚本content.js永远不会被调用。我不知道这是为什么。请帮忙。 也只会刷新一次,固定定时器后如何保持引用。
下面是manifest.json的
{
"name": "Refresher",
"version": "0.0.1",
"manifest_version": 2,
"description" : "Refresh the site contents after a while",
"icons": { "32": "icon.jpg" },
"browser_action": {
"default_icon": { "32": "icon.jpg" },
"default_title": "Refersher",
"default_popup": "browseraction/popup.html"
},
"background": {
"scripts": ["background.js"],
"persistent": true
},
"content_scripts": [{
"matches": ["http://www.w3schools.com/html/tryit.asp?filename=tryhtml_table"],
"js": ["content.js"]
}]
}
将'manifest.json'添加到问题中。 – wOxxOm
@ wOxxOm新增。请帮忙 – GitCoder