简体   繁体   English

在chrome扩展中创建HTML对话框弹出窗口

[英]Creating an HTML dialog pop-up in chrome extension

I'm working on a Chrome extension that will pop up a dialog box in the center of the screen when the user hits a keyboard shortcut. 我正在使用Chrome扩展程序,当用户点击键盘快捷键时,会在屏幕中央弹出一个对话框。 It will then use JavaScript to asynchronously load in content from the MediaWiki API. 然后,它将使用JavaScript异步加载MediaWiki API中的内容。 But I'm having difficulty figuring out how to create and display a dialog with JavaScript. 但我很难弄清楚如何使用JavaScript创建和显示对话框。 I don't want to use the Chrome html-popup browser action, because it appears off in the corner of the screen. 我不想使用Chrome html-popup浏览器操作,因为它出现在屏幕的一角。

I know how to use JavaScript to display an existing HTML dialog box, as this answer explains , but I don't know how to insert one into the DOM. 我知道如何使用JavaScript来显示现有的HTML对话框,正如这个答案所解释的那样 ,但我不知道如何在DOM中插入一个。 I don't want to use JavaScript's alert function, since that opens a separate window. 我不想使用JavaScript的alert功能,因为这会打开一个单独的窗口。 So is there a way to create and display an HTML modal dialog when an event triggers a JavaScript function in a chrome extension? 当事件触发chrome扩展中的JavaScript函数时,有没有办法创建和显示HTML模式对话框?

You should be able to use the javascript for the answer you linked to open that dialog whenever you want by injecting your javascript via a content script. 您应该可以使用javascript作为您链接的答案,通过内容脚本注入您的javascript,随时打开该对话框。 Google has documentation for this here: https://developer.chrome.com/extensions/content_scripts Google在此处提供了相关文档: https//developer.chrome.com/extensions/content_scripts

Basically, a content script runs on whatever pages you tell it to. 基本上,内容脚本在您告诉它的任何页面上运行。 So you could tell it to run on all web pages (configured via the manifest). 所以你可以告诉它在所有网页上运行(通过清单配置)。 This content script would add your listener, and then append your dialog to the body tage (or wherever, body is usually safe). 此内容脚本将添加您的侦听器,然后将您的对话框附加到body tage(或者在任何地方,body通常是安全的)。

The code would look something like: 代码看起来像:

$(document).on( 'keypress', 'body', function( e ){
    var code = e.keyCode || e.which;

    if( code = /*whatever, key shortcut listener*/ ) {
        document.body.innerHTML += '<dialog>This is a dialog.<br><button>Close</button></dialog>';
        var dialog = document.querySelector("dialog")
        dialog.querySelector("button").addEventListener("click", function() {
            dialog.close()
        })
        dialog.showModal()
    }
});

You may want to add safety code to check for your body tag; 您可能需要添加安全代码来检查您的身体标签; it's on normal pages but specialty pages may error (such as chrome://*). 它在普通页面上,但专业页面可能会出错(例如chrome:// *)。

As long as this runs on your content script, and your content script runs on your desired pages, you can run whatever listeners/dom changers you want this way. 只要它在您的内容脚本上运行,并且您的内容脚本在您想要的页面上运行,您就可以以这种方式运行您想要的任何侦听器/ dom转换器。

You don't need to add a permission for a public api. 您无需为公共API添加权限。 You only need to add a permission if the site doesn't allow cross origin requests. 如果站点不允许跨源请求,则只需添加权限。

Also, adding the listener for your keyboard shortcut using a web listener through a content script is not a good solution, since it requires a permission warning, and is generally not efficient. 此外,通过内容脚本使用Web侦听器为键盘快捷方式添加侦听器不是一个好的解决方案,因为它需要权限警告,并且通常效率不高。 Instead, you should use Chrome's commands api with activetab . 相反,您应该使用Chrome的命令 api和activetab Your extension will only be started when the user hits the keyboard shortcut, and the user can customize the shortcut via the shortcuts link at the bottom of chrome://extensions. 您的扩展程序仅在用户点击键盘快捷键时启动,用户可以通过chrome:// extensions底部的快捷方式链接自定义快捷方式。

To do this, add "permissions": [ "activeTab" ] to your manifest. 为此,请将"permissions": [ "activeTab" ]到清单中。 Then add your key combo: 然后添加你的密钥组合:

"commands": { "showcontentdialog": { "suggested_key": { "default": "Ctrl+Shift+Y" }, "description": "show content dialog" } }

Next, setup your background page listener and inject your content script when the user hits those keys: 接下来,设置后台页面侦听器并在用户点击这些键时注入内容脚本:

chrome.commands.onCommand.addListener(function(command) {
  if(command.name == "showcontentdialog") {
    chrome.tabs.executeScript({ file: "main.js" })
  }
})

Also, you should use appendChild instead of setting innerHTML, like this: 此外,您应该使用appendChild而不是设置innerHTML,如下所示:

var dialog = document.createElement("dialog")
dialog.textContent = "This is a dialog"
var button = document.createElement("button")
button.textContent = "Close"
dialog.appendChild(button)
button.addEventListener("click", function() {
  dialog.close()
})
document.body.appendChild(dialog)
dialog.showModal()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM