javascript - 创建第一个窗口时 chrome.windows.onCreated.addListener() 未运行
问题描述
我希望窗口在首次创建窗口时提示用户。如果用户创建另一个新窗口(例如:将选项卡拉出窗口以创建自己的窗口,或再次单击 chrome 图标),我的代码会提示用户,但在创建第一个窗口时不会。为什么是这样?
后台脚本:
//an array of current open windows.
var windowArray = {};
//when a new window is created, start the creation of a new package.
chrome.windows.onCreated.addListener(createWindowPackage);
//get current window's id
function getCurrentWinID(callback){
console.log("getCurrentWinID() called")
var windId;
chrome.windows.getCurrent(function(currentWin){
windId = currentWin.id;
//console.log("from getCurrentWinID(), windId = " + windId)
//*****unique window Id properly getting retrieved here
callback(windId);
});
}
//_______________________________________________________________________________________
//start the creation of a new 'windowPackage' object by prompting the user for a goal.
function createWindowPackage(){
var goal = prompt("What is your goal this browsing session?","E.g: To watch Daniel Schiffman's first video.");
var windId;
//getting the current window's id
getCurrentWinID(function(windId){
var winId = windId.toString();
//*****unique window id properly received here
//in the windowArray, have the windId key refer to a new windowPackage object.
windowArray[winId] = new windowPackage(goal, windId);
//*****so we've successfully created a new window package with this goal and a unique window Id
});
}
//_________________________________________________________________________________________
//a window package includes the window id and a tasks[] array
class windowPackage{
constructor(startTask, winId){
this.id = winId;//winId is a string right now.
this.tasks = [startTask];
this.goal = startTask;
this.isItOn = 1;
}
不知何故,但上面的代码不能在创建第一个新窗口时提示用户,但这段代码确实有效:
//an array of current open windows.
var goal;
chrome.windows.onCreated.addListener(askGoal);
function askGoal(){
goal = prompt("What is your goal this browsing session?","E.g: To watch Daniel Schiffman's first video.");
isItOn = 1;
}
解决方案
推荐阅读
- python - 有没有更简洁的方法来编写这段代码,你将如何绕过 keyerrors 和列出索引错误?
- java - 如何使用 informatica java 转换获取刷新令牌 google api
- caching - 如何删除/取消注册 hapi server.method 进行缓存
- java - Android——Handler的使用
- socket.io - 无法接收 SocketIO 客户端的“auth”参数
- python - 为什么这一直循环?尽管我更新了适当的变量
- angular - 当页面有 3 个网格时如何自动扩展第二个网格:Angular Kendo 网格
- express - 来自快速会话的会话不会通过请求持续存在
- java - 有什么方法可以初始化像 2.0.4 这样的无效 Spring Boot 版本
- sql - 是否可以对一张表创建一个 SQL SELECT 语句并取回两列及其条件?