- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试让一个简单的 Google Chrome 扩展程序工作,其中消息/变量流经以下每个步骤......
Background.js -> Popup.js
或
Background.js -> Contentscript.js
)发送消息/变量,但无法成功通过所有三个(
Contentscript.js -> Background.js -> Popup.js
)。这是我的演示中的文件。
<h1 class="name">Joe Blow</h1>
fromDOM = $('h1.name').text();
chrome.runtime.sendMessage({contentscript: "from: contentscript.js", title: fromDOM}, function(b) {
console.log('on: contentscript.js === ' + b.background);
});
chrome.tabs.getSelected(null, function(tab) {
chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) {
sendResponse({background: "from: background.js"});
console.log('on: background.js === ' + msg.title);
});
});
chrome.extension.sendMessage({pop: "from: popup.js"}, function(b){
console.log('on: popup.js === ' + b.background);
$('.output').text(b.background);
});
<html>
<head>
<script src="jquery.js"></script>
<script src="popup.js"></script>
</head>
<body>
<p class="output"></p>
</body>
</html>
{
"name": "Hello World",
"version": "1.0",
"manifest_version": 2,
"description": "My first Chrome extension.",
"background" : {
"scripts": ["background.js"]
},
"permissions": [
"tabs"
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["jquery.js","contentscript.js"],
"run_at": "document_end"
}
]
}
manifest_version: 2
的文档严重缺乏它很难破译。一个简单的、可重用的示例在学习过程中会非常有帮助,因为我确信这是一个常见问题。
最佳答案
好的,更改代码中的一些内容应该可以使其按您想要的方式工作。并非我将要进行的所有更改都是必需的,但这就是我可能会这样做的方式。
内容脚本
var fromDOM = $('h1.name').text();
chrome.runtime.sendMessage({method:'setTitle',title:fromDOM});
var title;
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
if(message.method == 'setTitle')
title = message.title;
else if(message.method == 'getTitle')
sendResponse(title);
});
chrome.runtime.sendMessage({method:'getTitle'}, function(response){
$('.output').text(response);
});
关于google-chrome-extension - Chrome 扩展 - 从 DOM 到 Popup.js 消息传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16322830/
希望你能帮助我。我阅读并再次阅读了 Magnific Popup 的文档,但我对 javascript 还不够好。 1- 我用这个链接打开一个弹出窗口: OPEN POPUP 和这个 javascri
我最近刚刚实现了“Magnific Popup”并且弹出窗口很好,但是当我单击一个输入框时,整个弹出窗口消失回到父页面。在插件网站上显示的示例中,整个对话框都是可单击的,直到您在该框外单击。 我希望它
我的弹出窗口正常工作,但有时用户会点击浏览器上的后退按钮关闭弹出窗口。 如何让浏览器后退按钮关闭已经打开的“magnific-popup”? 谢谢 最佳答案 经过一些挖掘发现 history.js 然
我制作了一个带有华丽弹出窗口的视频库。 但与图片库不同的是,视频库不显示计数器,例如1/3 在视频的右下角。为什么不?在图片库中,它运行良好。 视频库代码: $('.gallery_video').e
我正在使用 magnific popup 和 ajax 将内容加载到其中,并通过将查询字符串附加到 url 将值传递给 ajax 内容,除了在 IE7(可能还有 IE8)中工作正常。原因很可能是查询字
有没有办法绑定(bind)一个事件回调,以便为 Magnific Popup 的所有实例调用它?在我正在构建的网站上,当主浏览器滚动条消失时,几个元素具有固定位置和“跳转”。我想设置一个回调以在打开任
$.magnificPopup.open({ items: { src: $('#cabinet-msg') }, type:'inline', mid
在 jquery 对话框中我们可以做 $("#id").dialog('open|close') 打开关闭对话框。如何在 Magnific-popup 中做到这一点? 最佳答案 $.magnificP
我正在以这种方式制作新窗口: var WSHShell = WScript.CreateObject("WScript.Shell"); WSHShell.Popup("This is popup."
我打开一个带有 js 弹出窗口的 html 页面 Link to popup 现在我的弹出窗口中有一些链接,我想关闭我的链接的弹出窗口 onclick 并在旧窗口中打开链接。对于旧窗口,我指的是
无法在放大的弹出窗口中播放视频。 当我使用 class=iframe ,页面将重定向到 youtube 并播放视频。 但是当我使用 class=popup-youtube ,将显示弹出窗口,但我收到以
我有一个 Leaflet Popup 附加到一个标记,它给用户两个按钮来使用 jQuery 取消或移动到另一个页面。 这些按钮最初工作正常,除非您曾经准确地单击标记以使 Popup 消失,然后再次单击
我有 2 个 JQM 页面和 1 个它们共享的弹出窗口。我有一个事件处理程序绑定(bind)到两者的“pagecreate”,里面有这样的代码: $(document).on("pagecreate"
System.InvalidOperationException: You MUST call Rg.Plugins.Popup.Popup.Init(); prior to using it. 我已
测试 Magnific Popup 当前是否“打开”的最佳方法是什么? 检查某个 div 的存在是否会成为 future 的证明? 例如,如果我使用: if ($(".mfp-ready").leng
我的指令有问题。如果我从弹出 Controller 调用 $scope.popup.show = false 行,它就可以正常工作。如果我从子 contentDirective 调用 $scope.h
我在popup.js中有两个变量。我想把它们放在一起到一个 url ,无论如何就像 chrome.tabs.create({active: true, url: "https://"+savedApi
我有一些代码,它允许用户检查复选框并在提交时打开与新选项卡中的复选框关联的网址。如果两个复选框都被选中,则会打开两个选项卡。它的工作原理与预期一致,看起来像: document.getElementB
我有一个弹出窗口,其高度约为屏幕的 70%。我在这个弹出窗口中有 2 个 div, header-div 具有固定高度,例如20px 和一个 content-div,它应该使用剩余的可用高度。 目前我
我正在构建一个包含教程的 MMO。 当用户在教程中尝试离开教程或关闭选项卡/浏览器时,我需要显示一个自定义弹出窗口,其中包含有关用户退出教程的原因的选项。 我熟悉 onbeforeunload 事件,
我是一名优秀的程序员,十分优秀!