Chrome扩展程序 - 从javascript文件打开对话框(在content_script中配置)

时间:2016-08-04 08:28:28

标签: javascript google-chrome-extension

我有一个关于来自javascript文件的打开对话框的问题(content_script中的config)。我使用“chrome.windows.create”并有错误:“无法读取属性'创建'未定义”。你知道吗? 非常感谢你! 我的消息来源:

**manifest.json**
{
  "name": "ABC",
  "short_name": "ABC",
  "description": "My tool",
  "permissions": [ "contentSettings", "tabs", "http://*/*", "https://*/*" , "https://localhost/*", "https://localhost/*/*",  "contextMenus"],
  "homepage_url": "http://www.localhost/GMS",
  "update_url": "https://clients2.google.com/service/update2/crx",
  "manifest_version": 2,  
  "version": "1.0.4",
  "icons": {   
    "16": "img/icon-16.png",
    "48": "img/icon-48.png"
  },
  "browser_action": {
    "default_icon": "img/icon-128.png",
    "default_popup": "background.html"
  },
  "content_scripts": [{
    "css": [ "addon_tool.css" ],
    "js": [ "jquery.js", "config.js","myJs.js"]
  }]
}

**myJS.js**
 $.ajax({
            type: "POST",
            data:{UserName: 'name'},
            dataType: "json",
            url: "https://localhost/...",
            success: function (data) {
                alert('OK');
            },
            error: function (xhr, status, error) {
                try {
                    ***chrome.windows.create({ 'url': 'PopupForm.html', 'type': 'popup' }, function (window) {***
                    });
                } catch (e) {
                    alert(e.message);
                }
            }
        });
**PopupForm.html**
<!DOCTYPE html>
<html>
<head>
    <title></title>
	<meta charset="utf-8" />
</head>
<body>
    <button id="btnSubmit" >Submit</button>
    <button id="btnCancel" >Close</button>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

chrome.windows.* can't be accessed in content scripts,您需要将该逻辑移至弹出页面或背景页面。

  1. background.html重命名为popup.html。以前的名称表示您可能对背景页面和弹出页面感到困惑。
  2. 添加background page
  3. 将您的myJS逻辑移至背景页。