cha*_*ang 33 javascript popup google-chrome-extension
我正在编写Chrome扩展程序,当用户点击上下文菜单时,我希望弹出一个登录窗口,以便用户可以输入用户名和密码.在Chrome扩展,我只找到chrome.pageAction.setPopup
并且chrome.browserAction.setPopup
可以用来显示弹出式窗口,但他们表现出只有在点击页面操作的图标或浏览器操作的图标,而不是上下文菜单弹出窗口.当然,我可以使用javascript提示框来执行此操作,但问题是密码无法在提示框中屏蔽.所以我想知道是否还有其他方法可以在Chrome扩展程序中创建弹出窗口.
谢谢!
Rob*_*b W 93
挑选:
showModalDialog(<String url> [, <object arguments>])
window.open(<String url> [, <String window_name>[, <String windowFeatures>]])
chrome.windows.create(<object createData [, <function callback>]>)
所有这些方法都允许您(您的扩展程序)打开一个新窗口/对话框,并处理该页面中的逻辑.此页面应与您的扩展程序一起打包.
请参阅消息传递以将输入的数据传递到您的分机.
扩展程序中的标签可以使用直接访问后台页面chrome.runtime.getBackgroundPage
.我将在此演示中演示此功能,以及传统的消息传递方式:
manifest.json
{
"name": "Dialog tester",
"version": "1.0",
"manifest_version": 2,
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["open-dialog.js"]
}]
}
Run Code Online (Sandbox Code Playgroud)
background.js
// Handle requests for passwords
chrome.runtime.onMessage.addListener(function(request) {
if (request.type === 'request_password') {
chrome.tabs.create({
url: chrome.extension.getURL('dialog.html'),
active: false
}, function(tab) {
// After the tab has been created, open a window to inject the tab
chrome.windows.create({
tabId: tab.id,
type: 'popup',
focused: true
// incognito, top, left, ...
});
});
}
});
function setPassword(password) {
// Do something, eg..:
console.log(password);
};
Run Code Online (Sandbox Code Playgroud)
open-dialog.js
if (confirm('Open dialog for testing?'))
chrome.runtime.sendMessage({type:'request_password'});
Run Code Online (Sandbox Code Playgroud)
dialog.html
<!DOCTYPE html><html><head><title>Dialog test</title></head><body>
<form>
<input id="pass" type="password">
<input type="submit" value="OK">
</form>
<script src="dialog.js"></script>
</body></html>
Run Code Online (Sandbox Code Playgroud)
dialog.js
document.forms[0].onsubmit = function(e) {
e.preventDefault(); // Prevent submission
var password = document.getElementById('pass').value;
chrome.runtime.getBackgroundPage(function(bgWindow) {
bgWindow.setPassword(password);
window.close(); // Close dialog
});
};
Run Code Online (Sandbox Code Playgroud)
chrome.runtime.sendMessage(<request>, <function callback>)
和 chrome.runtime.onMessage
.addListener(<function listener>)
chrome.extension.getURL(<String path>)
chrome.runtime.getBackgroundPage(<function callback>)
chrome.tabs.create(<object createData> [, <function callback>])
chrome.windows.create(<object createProperties> [, <function callback>])
归档时间: |
|
查看次数: |
70510 次 |
最近记录: |