如何在新打开的Chrome标签上执行我的内容脚本? [英] How to execute my content script on a newly opened chrome tab?

查看:282
本文介绍了如何在新打开的Chrome标签上执行我的内容脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从门户网站调用Chrome扩展程序,以在新标签页中打开URL,在新打开的标签页中,我要执行executeScript:

I am calling a Chrome extension from a web portal to open a URL in a new tab, and on the newly opened tab I want to perform executeScript:

manifest.json

"externally_connectable": {
  "matches": ["http://localhost:3000/*"]
},
"permissions": ["tabs", "http://*/*", "https://*/*"]

background.js

// listen to webportal
chrome.runtime.onMessageExternal.addListener(
  function(request, sender, sendResponse) {
    a = chrome.tabs.create({ url: request.openUrlInEditor });
    chrome.tabs.insertCSS(a.id, { file: "combined.css" });
    chrome.tabs.executeScript(a.id, { file: "combined.js" });
  }
);

如果我尝试在扩展名点击上执行insertCSSexecuteScript, 效果很好

If I try to perform insertCSS and executeScript on extension click, it works fine

background.js

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.insertCSS(tab.id, { file: "combined.css" });
  chrome.tabs.executeScript(tab.id, { file: "combined.js" });
});

推荐答案

我自己解决了我的问题 向我的扩展程序授予活动标签页权限后,工作了 manifest.json

i solved my problem myself worked after giving active tabs permission to my extension manifest.json

    "permissions": ["tabs", "http://*/*", "https://*/*","activeTab"]

backround.js

// listen to webportal
chrome.runtime.onMessageExternal.addListener(
  function(request, sender, sendResponse) {
    chrome.tabs.create({ url: request.openUrl },function(tab) {
        chrome.tabs.insertCSS(tab.id, { file: "combined.css" });
        chrome.tabs.executeScript(tab.id, { file: "combined.js" });
    });
  }
);

这篇关于如何在新打开的Chrome标签上执行我的内容脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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