0

I need to close a specific tab in Google Chrome. The behaviour is that an extension open up URL after she loaded and this can't be avoid.

This doesn't work :

#manifest.json
 {
    "name": "Close Tab Helpx Adobe",
    "description": "Close the url http://www.example.com",
    "version": "0.1",
    "manifest_version": 2,
    "app": {
        "background": {
            "scripts": ["background.js"],
            "persistent": false
        }
    },
    "icons": {
        "16": "close-tab-helpx-adobe-16.png",
        "128": "close-tab-helpx-adobe-128.png"
    },
    "permissions": [
        "tabs"
    ]
 }

#background.js
chrome.tabs.onUpdated.addListener(function(tab) {
        if(tab.url=="http://www.example.com") {
            chrome.tabs.remove(tab)
        }
});

In developper mode, i can see Uncaught TypeError: Cannot read property 'onUpdated' of undefined

As you can see, i'm a beginner. Do you know how to achieve this ?

EDIT: I also tried :

#background.js
chrome.tabs.onActivated.addListener(function(activeInfo) {
    chrome.tabs.get(activeInfo.tabId, function(tab){
        if(tab.url=="http://www.example.com") {
            chrome.tabs.remove(tab);
        }
    });
}); 

chrome.tabs.getCurrent(function(tab){
    if(tab.url=="http://www.example.com") {
        chrome.tabs.remove(tab);
    }
});

chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
    if(tabs[0].url=="http://www.example.com") {
        chrome.tabs.remove(tabs[0]);
    }
});

The error is the same, only the property name change onActivated, getcurrent or query

Yann F.
  • 81
  • 1
  • 12
  • Without seeing manifest.json I can only guess you're trying to do it in a content script, which you can't. This API must be used in a [background script](https://developer.chrome.com/extensions/background_pages). Also always refer to the documentation of each event and method: your parameters for both onUpdated listener and remove() method are incorrect. – wOxxOm Jul 17 '20 at 04:00
  • Thanks, before asking i tried a lot of method. In my case, thoses lines come from background.js (i've just follow the chrome documentation and my searches give me some clues to write those lines). It seems that i need to search more, sorry for asking... – Yann F. Jul 17 '20 at 18:21
  • I guess manifest.json's `"permissions"` list doesn't have `"tabs"`? – wOxxOm Jul 18 '20 at 03:53
  • Nice try. Wrong guess ^^. I've just edit the post with manifest.json... Thank you for your advices. – Yann F. Jul 19 '20 at 19:44
  • 1
    You have a Chrome app, not an extension. Apps can't use API for extensions. If you did it by accident then remove `"app"` section and declare a [background script](https://developer.chrome.com/extensions/background_pages) instead. – wOxxOm Jul 21 '20 at 02:20
  • The app section is removed from the manifest and there is no more error. Instead, nothing happens ^^ Thanks for your help @wOxxOm I have now to figure out why it doesn't work... – Yann F. Jul 21 '20 at 18:18
  • 1
    chrome.tabs.remove accepts an id or an array of ids, not a tab object. – wOxxOm Jul 22 '20 at 05:05

1 Answers1

0

Thanks to wOxxOm (and his patience), here is a code which do the job :

#manifest.json
 {
    "name": "Close Tab Helpx Adobe",
    "description": "Close the url http://www.example.com",
    "version": "0.1",
    "manifest_version": 2,
    "background": {
            "scripts": ["background.js"],
            "persistent": false
    },
    "icons": {
        "16": "close-tab-helpx-adobe-16.png",
        "128": "close-tab-helpx-adobe-128.png"
    },
    "permissions": [
        "tabs"
    ]
 }

#background.js
    chrome.tabs.onCreated.addListener(function(tab) {
        chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
                if(tab.url=="http://www.example.com") {
                    chrome.tabs.remove(tab.id);
                }
        });
    });

So, i have my answer.

To go further ...

Haibara Ai says "Please be aware chrome.tabs.onUpdated will also fired for iframes, if a page contains many iframes, each completed iframe will trigger the event though you have checked : https://stackoverflow.com/a/36818991/2241806"

Yann F.
  • 81
  • 1
  • 12