Chrome扩展程序:尝试传递邮件,一旦收到该邮件,就会运行一项功能
16lz
2021-01-22
contentscript.js
function createIDArray()
{
var IDArray = [];
IDArray[0] = $('#u_4_0 > span > div').html();
return IDArray;
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log("This is being received from popup.js");
if (request.greeting == "hello")
console.log(createIDArray());
sendResponse({farewell: "goodbye"});
return true;
});
popup.js
$('#9610').click
(
function()
{
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "This is data."}, function(response) {
console.log(response.farewell);
});
});
}
);
Not receiving any response between the two scripts. My end goal here is going to be upon clicking a button in the popup, activate a function in the content script.
两个脚本之间没有收到任何响应。我的最终目标是点击弹出窗口中的按钮,激活内容脚本中的一个功能。
popup.html
<head>
<script type="text/javascript" src="jquery.js"></script>
<script src="popup.js"></script>
</head>
<body>
<button id="9610">Copy SRT Data</button>
<button id="1691">Paste SRT Data</button>
</body>
manifest.json
{
"manifest_version": 2,
"name": "Task Auto Fill",
"description": "This extension shows a Google Image search result for the current page",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"name": "My extension",
"background": {
"scripts": ["background.js"]
},
"permissions": [
"https://our.intern.facebook.com/",
"tabs",
"activeTab"
],
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["myscript.js"]
}
]
}
1 个解决方案
#1
0
Please move your
<script>
tag inpopup.html
to the bottom of<body>
tag. Please be aware currently you are putting them inside<head>
and when$('#9610').click()
is executed, the<body>
is not constructed and you won't find the button at all.请将popup.html中的
Update
request.greeting == "hello"
in yourcontentscript.js
. Please keep it sync with the data you sent frompopup.js
在contentscript.js中更新request.greeting ==“hello”。请保持与您从popup.js发送的数据同步
更多相关文章
- 彻底解决IE8和IE9下ewebeditor上按钮无效的方法
- 如何在单击按钮时将桌面应用程序导航到系统中设置的默认邮件提供
- 浏览器环境下JavaScript脚本加载与执行探析之动态脚本与Ajax脚本
- 像C#一样,去写Javascript----记我的脚本库RockSniper.Scripts.js
- 如何在选择单选按钮时显示文本字段
- bootstrap入门【按钮式下拉菜单,输入框组】
- 在AngularJS中轻松控制dom - 单击按钮,然后将焦点设置为输入元素
- 无法从按钮onclick事件ASP.NET 4调用Javascript函数
- 隐藏加载更多记录按钮ASP.net MVC Ajax Javascript