gpt4 book ai didi

javascript - 如何在 Chrome 扩展程序中操作新窗口?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:26:35 24 4
gpt4 key购买 nike

我正在编写一个 chrome 扩展,允许用户从一个页面登录社交媒体网站。我能够创建一个新的隐身窗口,但无法在我创建的窗口内操作任何内容。我想为新窗口创建一个onload函数来执行jquery。感谢您让我指明了正确的方向!

最佳答案

新incognito操作引用以下演示window创建并向其中注入(inject)一些 jquery。

引用资料

list 文件

这用于绑定(bind)权限和注册后台页面到扩展。确保它具有所需的所有权限。

{
"name":"Hanlder for Incognito window",
"description":"http://stackoverflow.com/questions/14044338",
"version":"1",
"manifest_version":2,
"background":{
"scripts":["background.js"]
},
"permissions":["tabs","http://www.google.co.in/"]
}

后台.js

从后台页面将 jquery 注入(inject)新的隐身窗口。

var _tabId_To_Look_For;

// Create a new incognito Window with some arbitary URL and give it focus
chrome.windows.create({
"url": "http://www.google.co.in/",
"focused": true,
"incognito": true
}, function (window) {
// Trace tab id which is created with this query
_tabId_To_Look_For = window.tabs[0].id
});

// Add an event Listener for new tab created
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
// Inject script into chosen tab after it is loaded completely
if (tabId == _tabId_To_Look_For && changeInfo.status == "complete") {
// Inject Jquery and in current tab
chrome.tabs.executeScript(tabId, {
"file": "jquery.js"
}, function () {
// I am in call back
console.log("Injected some jquery ");
});
}
});

确保您已启用隐身访问。

enter image description here

输出

您将观察到一个注入(inject)了 jquery 的新窗口。

关于javascript - 如何在 Chrome 扩展程序中操作新窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14044338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com