简体   繁体   English

chrome.tabs.create无法在Chrome启动时使用

[英]chrome.tabs.create not working on Chrome startup

I have a simple Chrome extension, with just this little code inside background.js file: 我有一个简单的Chrome扩展程序,只有background.js文件中的这个小代码:

chrome.tabs.create({}, function (tab) {
    console.log(tab.id);
});

When I install it through developer mode, it creates a new tab and writes its id to console. 当我通过开发人员模式安装它时,它会创建一个新选项卡并将其id写入控制台。

But when I exit Chrome and start it again, nothing happens on Chrome startup.. I thought background scripts are loaded on each browser startup? 但是当我退出Chrome并再次启动时,Chrome启动时没有任何反应..我认为每个浏览器启动时都会加载后台脚本?

What is wrong with this and is there any other way to do this on browser startup? 这有什么问题,还有其他方法可以在浏览器启动时执行此操作吗?

Change your manifest and add background property: 更改清单并添加背景属性:

"background": {
    "scripts": ["background.js"]
  },

Chrome will run this script "background.js" on browser startup every time(till your extension enabled). Chrome每次都会在浏览器启动时运行此脚本“background.js”(直到启用扩展程序)。 So just add you code to background.js script. 所以只需将代码添加到background.js脚本中。

 //background.js content
    chrome.tabs.create({}, function (tab) {
        console.log(tab.id);
    });

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

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