我需要迭代并单击该类的所有元素.star_gray
在页面上,并在重定向后保持迭代和单击。运行JavaScript代码无法满足第二个要求,所以我打算写一个Chrome扩展。
但我未能通过扩展模拟网页上的点击事件。我的项目如下:
清单.json
{
"manifest_version": 2,
"name": "Check'em All",
"description": "",
"version": "1.0",
"browser_action": {
"default_popup": "popup.html"
},
"background": {
"persistent": true,
"scripts": ["jquery.js", "background.js"]
},
"content_scripts": [{
"matches": ["file:///*"],
"js" : ["popup.js"]
}],
"permissions": [
"tabs",
"http://*/*",
"https://*/*"
]
}
弹出窗口.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Check'em All!</title>
</head>
<body>
<h1>Check'em All!</h1>
<button id="check-btn">BUTTON</button>
<script src="http://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>
<script src="popup.js"></script>
</body>
</html>
popup.js
document.addEventListener('DOMContentLoaded', function () {
var btn = document.getElementById('check-btn');
btn.addEventListener('click', injectScript);
});
function injectScript() {
alert('Handler called!');
chrome.tabs.executeScript(null, { file: 'background.js' });
}
背景.js
$(document).ready(function(){
$('.star_gray').click();
$('a.btn.page_next').click();
});
使用上面的代码,当我单击按钮时(#check-btn
)在弹出窗口上,没有任何反应。