首页 文章

创建Chrome扩展程序以在新标签页中打开链接

提问于
浏览
10

我想创建一个简单的chrome扩展,点击它时会在新的浏览器选项卡中打开一个URL . 这就是我对manifest.jason的看法

{
    "name": "Sprout Social",
    "description": "Shortcut to Sprout Social",
    "permissions": [
        "tabs"
    ],
    "icons": {
        "128": "128.png"
    },
    "launch": {
        "web_url": "http://www.sproutsocial.com"
    }
}

任何帮助都会很棒 .

3 回答

  • 15

    好的,首先,manifest.json(不是jason)有一个严格的结构,你不能搞砸它 .

    https://developer.chrome.com/extensions/manifest.html

    您必须创建一个浏览器操作扩展,这意味着您的扩展程序将在工具按钮附近有一个按钮 .

    https://developer.chrome.com/extensions/browserAction.html

    您不需要任何popup.html,您可以跳过该部分 . 您需要编写后台页面,很多人将其命名为background.html此HTML文件将包含您的代码,格式如下:

    <html><head><script> your script here (use as many lines as you want)  </script></head>/html>
    

    这个HTML永远不会出现 .

    代码可以是你想要的任何东西,比如其他答案中的代码:

    chrome.browserAction.onClicked.addListener(function() {
        chrome.tabs.create({'url': "http://www.sproutsocial.com"});
    });
    

    就是这样 .

  • 5

    更简单的解决方案,您不需要HTML .

    将此添加到 manifest.json

    "browser_action": {
        "default_icon": "images/icon38.png",
        "default_title": "Your title"
    },
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    }
    

    使用以下代码创建 background.js 文件:

    chrome.browserAction.onClicked.addListener(function(tab) {
        chrome.tabs.create({ url: "http://www.yoursite.com" });
    });
    

    注意:我没有在manifest.json中添加 "permissions": ["tabs"] ,因为它添加了权限警告:"Read your browsing history",这可能会让用户感到困惑 . 扩展仍然有效 .

  • 2

    我认为你想在模式中定义的方法是

    chrome.tabs.create
    
     chrome.browserAction.onClicked.addListener(function() {
    
          chrome.tabs.create({'url': chrome.extension.getURL('popup.html')}, function(tab) {
    
          });
    
     });
    

相关问题