最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Display Dialog from Chrome Extension - Stack Overflow

programmeradmin13浏览0评论

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>

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>
Share Improve this question asked Jan 2, 2010 at 17:35 JasCavJasCav 34.7k21 gold badges116 silver badges174 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You cannot use a dialog like this in a background page:

background-pages

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 municate to the page itself. Therefore, you get the selectedTab via :

method-getselected

Once you get the tab, you can then execute the JavaScript:

method-executescript

To clarify Mohamed's answer a bit, closure's modal dialog is in-page HTML. This is probably actually working in your code, but since you're doing it in the background page, and the background page isn't visible, you don't see it. You can use techniques that use window.open or window.alert from the background page, but not things that are trying to display interactive HTML to the user. For that, you'll need to get the content into a popup window or into the page itself as Mohamed suggests.

发布评论

评论列表(0)

  1. 暂无评论