Chrome 扩展程序在新标签页上打开新标签页 [英] Chrome extension open new tab on new tab

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

问题描述

我创建了一个 Chrome 扩展程序,作为其操作的一部分,它会打开一个带有指定 url 的新标签页.

I have created a Chrome extension that, as part of it's operation, opens a new tab with a specified url.

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if( request.message === "open_new_tab" ) {
      chrome.tabs.create({"url": request.url});
    }
  }
);

(完整代码可在 GitHub 上获得)

(Full code available on GitHub)

这在带有网页的标签上工作正常,但我无法让它在空标签上工作,例如:chrome://apps/ 澄清一下,如果我打开了一个标签并且它是在 stackoverflow.com 上,然后当我单击我的扩展按钮时,它会打开一个新选项卡,加载生成的 url.当我在一个新选项卡上,或者一个 url 以 chrome:// 开头的选项卡上时,扩展程序不起作用.

This works fine on tabs with webpages, but I cannot get it to work on empty tabs, for example: chrome://apps/ To clarify, if I have a tab open and it is on stackoverflow.com, then when I click on my extension button it opens a new tab loading a generated url. When I am on a new tab, or a tab where the url begins with chrome:// then the extension does not work.

我需要包含哪些权限才能允许扩展程序在任何选项卡中打开?包括新标签和任何 chrome:// 标签?

What permissions do I need to include to allow the extension to open in ANY tab? Including new tabs and any chrome:// tab?

Manifest.json:

Manifest.json:

{
  "manifest_version": 2,

  "name": "MyMiniCity Checker",
    "short_name": "MyMiniCity Checker",
  "description": "Checks what your city needs most and redirects the browser accordingly.",
  "version": "0.2",
    "author":"Richard Parnaby-King",
    "homepage_url": "https://github.com/richard-parnaby-king/MyMiniCity-Checker/",
    "icons": {
      "128": "icon-big.png"
   },

    "options_page": "options/options.html",

  "browser_action": {
    "default_icon": "icon.png"
  },
    "permissions": ["tabs","storage","http://*.myminicity.com/","http://*/*", "https://*/*"],
    "background": {
    "scripts": ["background.js"],
    "persistent": false
    },
    "content_scripts": [ {
    "matches": [ "http://*/*", "https://*/*"],
    "js": [ "jquery-1.11.3.min.js" ]
  }]
}

背景.js:

//When user clicks on button, run script
chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(null, { file: "jquery-1.11.3.min.js" }, function() {
    chrome.tabs.executeScript(null, { file: "contentscript.js" });
    });
});

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if( request.message === "open_new_tab" ) {
      chrome.tabs.create({"url": request.url});
    }
  }
);

看起来好像没有执行 background.js 文件.我怀疑这是一个权限.我需要什么权限才能在每个选项卡中运行此扩展程序?

It appears as though the background.js file is not being executed. I suspect this to be a permissions. What permissions do I need in order to run this extension in every tab?

推荐答案

因为我希望它在每个页面上运行,这意味着我无法在内容脚本中使用代码.我把所有的代码都移到了后台脚本中:

As I was wanting this to run on EVERY page it meant I could not have the code in the content script. I moved all the code into the background script:

chrome.browserAction.onClicked.addListener(function(tab) {
        //... 
        chrome.tabs.create({"url": newTabUrl});
        //...
});

所以当我点击我的按钮时,上面的代码被调用,使用附带的 jquery 脚本.

So when I click on my button the above code is called, using the enclosed jquery script.

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

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