在我的manifest.json文件中有此内容
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["jquery.min.js", "content.js"],
"run_at": "document_end"
}
"background": {
"page": "background.html"
},
然后在background.html页面中,我调用一个外部javascript文件(content.js):
<html>
<head>
<meta charset=utf-8 />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="jquery.min.js"></script>
<script src="background.js"></script>
</head>
<body>
</body>
</html>
我从我的background.js向content.js发送一条消息:
chrome.tabs.sendMessage(tabs[0].id, {data: message}, function(response) {
...});
当我从页面更改dom时,我的content.js我有一个侦听器:
chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) {
var data = request.data || {};
$('.re-DetailFeatures').first().before('<button name="myButton">Contact</button>');
sendResponse({data: data, success: true});
});
我想做的是从content.js(称为myButton)在dom中创建的按钮调用javascript函数。