- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为 chrome 扩展制作一个弹出界面。我似乎无法从 popup.html/popup.js 向 content.js 脚本发送消息。这是我到目前为止所拥有的。当我点击扩展图标时,我会看到一个按钮,上面写着 clickme。我点击它,什么也没发生,chrome javascript 控制台中没有错误,也没有向 content.js 发送消息。
显现
{
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"manifest_version": 2,
"name": "extensiontest",
"version": "0.2",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["content.js"]
}
],
"browser_action": {
"default_icon": "Beaker.png",
"default_popup":"popup.html"
},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs"
]
}
<html>
<head></head>
<script src="popup.js"></script>
<body>
<input id="button1" type=button value=clickme>
</body></html>
function popup(){
alert(1);
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {"message": "start"});
});
button1=document.getElementById("button1");
button1.addEventListener('click', popup)
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "start" ) {
start();
}
}
);
function start(){
alert("started");
}
最佳答案
我修改了你的 popup.js
并使用 DOMContentLoaded
正如 Chrome 扩展程序所建议的那样:
popup.js:
function popup() {
chrome.tabs.query({currentWindow: true, active: true}, function (tabs){
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {"message": "start"});
});
}
document.addEventListener("DOMContentLoaded", function() {
document.getElementById("button1").addEventListener("click", popup);
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "start" ) {
start();
}
}
);
function start(){
alert("started");
}
<!DOCTYPE html>
<html>
<head></head>
<script src="popup.js"></script>
<body>
<input id="button1" type=button value=clickme>
</body></html>
关于google-chrome-extension - 从弹出窗口到 content.js 的 sendMessage 在 chrome 扩展中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29926598/
我正在编写 google chrome 扩展程序,并尝试将信息从注入(inject)网页的一段代码发送到我的内容脚本。 根据 http://developer.chrome.com/extension
我正在使用 websockets 和 Jetty 7.1.6.v20100715。 我有几个问题,主要是因为缺乏有关这些方法的信息/解释。 1st)sendMessage(字节帧,字符串数据)中的字节
我正在尝试调试我的 Windows 安装问题。细节并不特别重要,但我正在寻找以下问题的答案: explorer.exe receives a WM_SETTINGCHANGE message. In
我试图了解转换十六进制整数(十进制)和 IntrPtr 的规则是什么。 我在某处读到它应该代表“更高的内存”或类似的东西。 如果有人能向我解释一下,那就太好了。 但实际上它只是关于以下内容: 我想用S
我遇到了一个问题,即 SendMessage() 函数导致脚本挂起并因此永远不会退出,尽管它正在像它应该的那样运行 SendMessage(它的任务完成)。有没有办法解决这个问题,因为我有一段时间从主
我使用 InnoSetup 在注册表中设置环境变量: [Registry] Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session
我试图理解 SendMessage 函数,这是我的实际代码: [DllImport("user32.dll")] public static extern IntPtr SendMessage(Int
我正在将 Eclipse 与 ADT 结合使用,并且正在学习本教程:http://developer.android.com/training/basics/firstapp/starting-act
在使用 C++ 时,我已经花了很多时间试图解决这个问题。这段代码来自一个工作程序,我正在用 C# 重写它,但发生了一些我不明白的事情。 下面的代码正是我按下“Step Into”时运行的代码。现在使用
不确定为什么 SendMessage 没有从我需要的类中获取文本。我以前做过,但它是 VisualBasic,我想将它移植到 C++。我没有在任何其他程序上尝试过此代码。我正在阅读一些关于它可能是 u
在开始阅读问题之前请注意我阅读了 Petzold 的书(windows 编程第 5 版)! 在 form.h 中,我声明了名为 CMessage 的自定义消息(它代表“自定义消息”,稍后调用创建按钮的
我试图在两个应用程序之间传递消息 - 其中一个是插件,另一个是独立的配置实用程序。当我的插件检测到一个事件时,我想向我的实用程序发送一条消息并提示用户重新配置。 我使用的代码如下: [DllImpor
我正在尝试使用 sendmessage 将消息从我的 C++ 应用程序传递到 C# 我的c++代码是这样的 int _tmain(int argc, _TCHAR* argv[]) { COPYDA
为什么 Windows SendMessage() 总是返回零,即使消息传递成功?有没有办法用 SendMessage() 检查消息传递失败? 编辑 忘记提及我在 C++ DLL 中使用 SendMe
当我们发送消息时,“如果指定的窗口是由调用线程创建的,则立即将窗口过程作为子例程调用”。但是“如果指定的窗口是由不同的线程创建的,系统会切换到该线程并调用适当的窗口过程。只有当接收线程执行消息检索代码
我正在尝试通过 SendMessage(..) 方法将鼠标事件发送到 windows 中的窗口。 我面临的问题是消息似乎没有传送到我发送它们的窗口,即使 SendMessage 返回 0,这(根据文档
我已经成功地使用 Windows SendMessage 方法帮助我在我的文本编辑器中做各种事情,但每次我只是复制和粘贴别人建议的代码,我真的不知道它是什么意思。始终有一个神秘的消息编号作为参数。我如
我想从 MyServerProtocol 类外部调用 sendMessage 方法并向连接的客户端发送消息。我使用 threading 来做到这一点。 当我使用这段代码时: from autobahn
我正在编写 C# 自动化工具。 由于 Microsoft UI Automation 不提供任何模拟右键单击或弹出上下文菜单的方法,因此我使用 SendMessage 来代替。我宁愿不使用 SendI
我的问题是:如何使用 SendMessage() 实现两个线程之间的线程通信,一个有窗口(GUI),另一个没有窗口? 问题是 SendMessage() 需要一个句柄 (HWND)? 关于我的项目的另
我是一名优秀的程序员,十分优秀!