Chrome plug-in development-take the initiative to obtain the tabid of all tabs, background.js directly transmits messages to all tabs synchronously

Sometimes backgroud.js needs to synchronize messages to all tabs at the same time. At this time, it is necessary to obtain the tabid of all tabs . The following method can be implemented.

function open_all_tab(){
    
    
	// 获取所有的页签
	chrome.tabs.getAllInWindow(null, function(tabs){
    
    
		for (var i = 0; i < tabs.length; i++) {
    
    
			// 在控制台打印出页签的tabid
			console.log(tabs[i].id);
			// 通过tabid向每一个页签发送消息
			chrome.tabs.sendMessage(tabs[i].id, {
    
    type: 'xxx'});
		}
	});
}

Like it if you like it ❤!

Guess you like

Origin blog.csdn.net/qq_38161040/article/details/107772138