简体   繁体   English

在新打开的选项卡中运行脚本

[英]Run script in newly opened tab

I'm writing a chrome extension which will open new tab. 我正在编写一个Chrome扩展程序,它将打开新标签页。 My question is how can I get the newly opened tab? 我的问题是如何获得新打开的标签? When I reach that new tab, I want to make some changes in the source code. 当我到达新标签时,我想对源代码进行一些更改。 This; 这个; https://developer.chrome.com/extensions/tabs#method-create creates new tab. https://developer.chrome.com/extensions/tabs#method-create创建新标签。 But I want to make some changes before user click the tab. 但是我想在用户单击选项卡之前进行一些更改。 (Change the selected value and run inside function) (更改所选值并在函数内部运行)

How can I do this with method create? 我该如何使用方法创建?

There is a callback parameter for chrome.tabs.create , you could call chrome.tabs.executeScript inside it and inject some code into the created tab. chrome.tabs.create有一个回调参数,您可以在其中调用chrome.tabs.executeScript并将一些代码注入到创建的选项卡中。

chrome.tabs.create({url: 'http://www.google.com'}, function(tab) {
    chrome.tabs.executeScript(tab.id, {code: 'document.body.style.backgroundColor = 'green;'});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM