从Chrome扩展程序显示对话框 [英] Display Dialog from Chrome Extension
问题描述
我试图在用户创建书签时在Chrome中显示模式JavaScript对话框。但是,在尝试Closure和SimpleModal + JQuery之后,我似乎无法看到一个对话框出现。这是Chrome中扩展的限制,还是我做错了什么? (我仍然在学习Javascript,所以我不理解它可能是原因。)
这是我的代码,使用Closure。它确实把它变成了函数,所以这个工作没问题。有什么建议么?谢谢!
< html>
< head>
< script src =./ lib / closure-library / closure / goog / base.js>< / script>
< script type =text / javascriptsrc =./ lib / closure-library / closure / goog / deps.js>< / script>
< script> goog.require('goog.ui.Dialog');< / script>
< script type =text / javascript>
chrome.bookmarks.onCreated.addListener(function(id,bookmark){
//设置对话框
var dialog1 = new goog.ui.Dialog();
dialog1.setContent('[Insert Placeholder]');
dialog1.setTitle('Title Placeholder');
//显示对话框
dialog1.setVisible(true);
});
< / script>
< / head>
< body>
<! - 无所事事 - >
< / body>
< / html>
在后台页面中不能使用这样的对话框:
您可以为选项页面做到这一点:
所以在你的情况下,你会想听书创建的,因为你想做一个对话框,你需要与页面本身进行通信。因此,您可以通过以下方式获取selectedTab:
获得标签后,您可以执行JavaScript:
I am trying to display a modal Javascript dialog box in Chrome when a user creates a bookmark. However, after trying Closure and SimpleModal+JQuery, I can't seem to get a dialog box to appear. Is this a restriction of extensions in Chrome, or am I doing something very wrong? (I'm still learning Javascript, so my lack of understanding it could very well be the reason.)
Here is my code using Closure. It does make it into the function, so that's working okay. Any suggestions? Thanks!
<html>
<head>
<script src="./lib/closure-library/closure/goog/base.js"></script>
<script type="text/javascript" src="./lib/closure-library/closure/goog/deps.js"></script>
<script>goog.require('goog.ui.Dialog');</script>
<script type="text/javascript">
chrome.bookmarks.onCreated.addListener(function(id, bookmark) {
// Setup the dialog box.
var dialog1 = new goog.ui.Dialog();
dialog1.setContent('[Insert Placeholder]');
dialog1.setTitle('Title Placeholder');
// Display dialog.
dialog1.setVisible(true);
});
</script>
</head>
<body>
<!-- Do Nothing -->
</body>
</html>
You cannot use a dialog like this in a background page:
You can do that for options page:
Google Chrome Extensions Options
So in your case, you would want to listen onCreated for bookmarks, and since you want to do a dialog box, you would need to communicate to the page itself. Therefore, you get the selectedTab via :
Once you get the tab, you can then execute the JavaScript:
这篇关于从Chrome扩展程序显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!