Chrome扩展:如何使当前窗口的对话框中心
chrome extension: how to make dialog center of current window?
chrome.browserAction.onClicked.addListener(function (tab) {
var currentWidth = 0;
var currentHeight = 0;
chrome.windows.getCurrent(function(w) {
// You can modify some width/height here
alert(w.top);
currentWidth = w.left / 2;
currentHeight = w.top / 2;
});
var w = 440;
var h = 220;
var left = (screen.width / 2) - (w / 2) - currentWidth;
var top = (screen.height / 2) - (h / 2) - currentHeight;
chrome.windows.create({
'type': 'popup',
'width': w,
'height': h,
'left': left,
'top': top}, function (window) {
chrome.tabs.executeScript(newWindow.tabs[0].id, {
code: 'document.write("hello world");'
});
});
});
窗口显示在中间,但是当当前窗口的大小被调整到一个较小的视图时,窗口显示在屏幕中心不是当前窗口中心的地方。
当我从currentHeight或currentWidth中删除/2
时,窗口显示,但它位于错误的位置(离一侧太远)。
你在滥用异步API。
请参阅这个问题的一般描述。
在您的例子中,将逻辑移动到第一个回调中应该足够了:
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.windows.getCurrent(function(win) {
var currentWidth = 0;
var currentHeight = 0;
var width = 440;
var height = 220;
// You can modify some width/height here
alert(win.top);
currentWidth = win.left / 2;
currentHeight = win.top / 2;
var left = (screen.width / 2) - (width / 2) - currentWidth;
var top = (screen.height / 2) - (height / 2) - currentHeight;
chrome.windows.create(
{
'type': 'popup',
'width': width,
'height': height,
'left': left,
'top': top
}, function (window) {
chrome.tabs.executeScript(window.tabs[0].id, {
code: 'document.write("hello world");'
});
}
);
});
// Here, currentWidth/currentHeight is not assigned yet
});
相关文章:
- 在 Telerik RadGrid 中单击复选框时打开对话框窗口
- 当对话框窗口打开时,使父页模糊
- 如何使用JavaScript关闭此对话框窗口
- Ajax 请求未拉出 jQuery 对话框窗口
- Cke对话框窗口增加了简单的功能
- Ajax 对对话框窗口的响应在第二次尝试时不显示更改的值
- 堆栈溢出如何创建模式对话框窗口
- 如何从IE和Chrome浏览器中的显示模式对话框窗口返回数组对象
- Facebook:对话框窗口弹出 - 导航离开时,我该怎么做
- CKEditor:在插件“图像”的对话框窗口中设置输入字段的值
- 根据 ScriptUI 中的操作更改对话框窗口的大小
- 将选定行的数据从对话框窗口传递到主页
- 模式对话框窗口中的不可见融合图未导出
- 从jquery移动对话框窗口执行父函数
- Indesign Script JS:编辑文档文本框内容并返回对话框窗口
- 通过javascript控制自定义对话框窗口的字段
- 如何保存对话框窗口的位置
- Javascript 对话框窗口关闭
- 年龄验证-关闭对话框窗口
- 如何打开一个对话框窗口