【问题标题】:Open new tab from chrome extension从 chrome 扩展打开新标签
【发布时间】:2011-06-26 10:40:43
【问题描述】:

我想创建一个 chrome 扩展程序,当您单击它时,您会打开我的网站。但是怎么做? 我尝试在谷歌上搜索,但这就是我能创造的全部:

{
  "name": "My First Extension",
  "version": "1.0",
  "description": "The first extension that I made.",
  "browser_action": {
    "default_icon": "icon.png"
    chrome.tabs.create({'url': chrome.extension.getURL('popup.html')}, function(tab) {
        // Tab opened.
    });
  },
  "permissions": [
    "http://api.flickr.com/"
  ]
}

但这似乎不起作用。

【问题讨论】:

标签: json google-chrome google-chrome-extension


【解决方案1】:

一小段代码,

chrome.tabs.create({'url': chrome.extension.getURL('popup.html')}, function(tab) {
    // Tab opened.
});

不能直接在清单中使用。如果您查看documentation 了解如何使用浏览器操作,处理点击事件的正确方法是在后台页面的 JavaScript 中放置类似这样的内容:

chrome.browserAction.onClicked.addListener(function() {

    chrome.tabs.create({'url': chrome.extension.getURL('popup.html')}, function(tab) {
        // Tab opened.
    });

});

从您的清单来看,您似乎没有背景页面。只需创建一个包含一些 JavaScript 的 HTML 文件,然后在清单中引用它,如下所示:

"background_page" : "background.html"

【讨论】:

    【解决方案2】:
    {"manifest_version": 2,"name": "my app","version": "2.0","description": "This is a chrome extension for my app","browser_action": {"default_icon": "icon.png","default_popup": "popup.html"},"permissions": ["www.bibsoblog.blogspot.com"]}
    

    如果您想通过单击扩展来创建扩展以访问您的网站,只需在此处使用此代码click this to add that code to your popup.html

    【讨论】:

      猜你喜欢
      • 2012-03-23
      • 1970-01-01
      • 2015-05-17
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      相关资源
      最近更新 更多