Reputation: 109
I would like to prevent new tab creation. Either by clicking 'new tab button' either by window.open method. Is there any way to listen on new tab creation. I don't mean 'targetcreated' event because then tab is already created. Maybe there is something similar to event.preventDefault?
Below is a very simple code that closes every new tab but I think it's very ugly and I want to improve it.
browser.on('targetcreated', (target) => {
function closeNewTabs(target) {
let targetBrowser = target.browser();
targetBrowser.pages().then(data => {
if (data.length>1) {
for (let i = 0; i < data.length; i++) {
if (i>1) data[i].close();
}
}
})
}
})
Upvotes: 2
Views: 4196
Reputation: 4064
A possible workaround would be to listen to targetcreated event and then page.close
It would be something similar to
browser.on("targetcreated", async (target)=>{
const page=await target.page();
if(page) page.close();
});
It doesn't prevent creating new tabs, but it should close them immediately after opening
Upvotes: 5