从Chrome扩展程序中打开新标签页 [英] Open new tab from chrome extension

查看:110
本文介绍了从Chrome扩展程序中打开新标签页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Chrome扩展程序,当您单击该扩展程序时,您可以打开我的网站.但是如何?我尝试在Google上进行搜索,但这就是我可以创建的所有内容:

I wanna create a chrome extension that when you click it that you open my website. But how? I tried searching on google but this is all what I could create:

{
  "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/"
  ]
}

但这似乎不起作用.

推荐答案

一段代码,

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

不能直接在清单中使用.如果您查看文档以了解如何使用浏览器操作,则正确的方法是处理click事件的方法是在背景页面的JavaScript中添加以下内容:

cannot be used directly in the manifest. If you look at the documentation for how to use browser actions, the correct way of handling the click event is to put something like this in the JavaScript on your background page:

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

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

});

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

From your manifest, it looks like you don't have a background page. Simply create an HTML file with some JavaScript in it, and reference it in the manifest like so:

"background_page" : "background.html"

这篇关于从Chrome扩展程序中打开新标签页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆