我正在使用该react-create-app
库来创建 google-chrome-extension。它尝试从文件中发送一条消息,content.js
并将App.js
其显示为警报和console.log。我没有错误。没有警报出现,console.log 中也没有显示任何内容。有人可以给你建议。扩展程序正常启动。
结构项目
project
build
node_modules
public
/assets
content.js
favicon.ico
index.html
manifest.json
src
App.js
.gitignore
package.json
清单.json
{
"manifest_version": 2,
"name": "chrome extension",
"description": "Chrome extension",
"version": "0.0.1",
"content_scripts": [
{
"js": ["content.js"],
}
],
"browser_action": {
"default_popup": "index.html",
"default_title": "Open the popup"
},
"icons": {
"16": "assets/icon-128.png",
"48": "assets/icon-128.png",
"128": "assets/icon-128.png"
},
"permissions": [],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}
内容.js
/*global chrome*/
var timer = 0;
var si = setInterval(() => {
try {
chrome.runtime.sendMessage(
{
data: 'Hello popup, how are you'
},
function(response) {
console.dir(response);
}
);
timer++;
if (timer === 5) {
clearInterval(si);
}
} catch (error) {
// debugger;
console.log(error);
}
}, 2000);
应用程序.js
/*global chrome*/
import * as React from 'react';
import Form from './components/form/Form';
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
alert('I am popup!');
console.log('I am popup');
sendResponse({
data: 'I am fine, thank you. How is life in the background?'
});
});
function App() {
return <Form />;
}
export default App;