2

I'm tring to get current tab id in content script. But it fails all the time. I'm not sure, what i'm doing false.

Here are some solutions from another topics, but these are not working in my extension:

CODE 1 - content.js

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    alert("sent from tab.id=", sender.tab.id);
});

CODE 2 - content.js

chrome.extension.sendRequest({
    action: "WhatYouWant"
});

chrome.extension.onRequest.addListener(function (request, sender, sendResponse) {
    if (request.action) {
        alert('The response is : ' + request.action);
    }
});

background.js

chrome.extension.onRequest.addListener(function (request, sender, sendResponse) {
    if (request.action) {
        // Make what you want
        chrome.tabs.getSelected(null, function (tabs) {
            chrome.tabs.sendRequest(tabs.id, {
                action: "response"
            });
        });
    }
});

manifest.json

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

"content_scripts": [{
    "all_frames": true,
    "js": ["content.js"],
    "matches": ["<all_urls>"],
    "run_at": "document_end"
}],
"web_accessible_resources": [
    "content.js"
],
...

Note: This is not duplicate topic, the solutions in other questions don't work for me.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
Micheal Toru
  • 422
  • 4
  • 28
  • Extension messaging doesn't deliver a message to the page that sends it. The usual method to get a tab id is to send a message to your background/event page which then responds with sender.tab.id. You can also add an iframe that points to your extension's page exposed via web_accessible_resources - the iframe can use chrome.tabs.getCurrent to get its own tab, then it can send to your content script via DOM message event i.e. window.parent.postMessage. – wOxxOm Jun 26 '18 at 12:59
  • The code 2 is actually does that. It sends a message to background and background responds with the tab info. But this code is also not working – Micheal Toru Jun 26 '18 at 13:02
  • I said sender.tab.id, but your code uses the deprecated chrome.tabs.getSelected which gives you just the active tab, which is not what you need. Note, a *separate instance* of your content script runs in any particular web page, active and inactive, it's not one instance unlike the background page. – wOxxOm Jun 26 '18 at 13:05
  • Example: [Obtaining "this" tab ID from content script in Chrome extension?](//stackoverflow.com/a/45600887) – wOxxOm Jun 26 '18 at 13:13
  • i just tested it the the following code in background listener: alert(sender.tab.id); this is also not working – Micheal Toru Jun 26 '18 at 13:15
  • The method are that topic is also not working for me. it returns always "undefined" – Micheal Toru Jun 26 '18 at 13:17
  • Without seeing MCVE of your last attempt, all I can say is that there are dozens of ways to do it wrong. – wOxxOm Jun 26 '18 at 13:17
  • 1
    Here's the complete working extension built from the example I've linked: https://puu.sh/AMeR3/4183febc3b.zip – wOxxOm Jun 26 '18 at 13:23
  • Did the extension work? Because i'm getting also here undefined .. – Micheal Toru Jun 26 '18 at 19:03
  • I tried now the same code in another computer, it doesnt work. – Micheal Toru Jun 26 '18 at 19:07
  • 1
    There's something wrong with how you try it, I guess. Install the extension, open or reload a web page like www.google.com, inspect the console. – wOxxOm Jun 26 '18 at 19:10
  • Also make sure your console doesn't have a custom filter applied, and "Default levels" is selected. – wOxxOm Jun 26 '18 at 19:11
  • 1
    You can also debug the code using devtools by setting breakpoints and inspecting the variables. – wOxxOm Jun 26 '18 at 19:12
  • 1
    This is as much help I can provide without a TeamViewer session or similar remote access, but that's not how StackOverflow works. – wOxxOm Jun 26 '18 at 19:13
  • Yes, that's true. I actually tried always with alert() function, but because of async it gets the value after the showing alert message. i tried it with console.log it's working now thanks. – Micheal Toru Jun 26 '18 at 19:13

2 Answers2

-1

The chrome.tabs API is not accessible inside a content script:

Use simply (javascript) :

location.href

to get the URL of the current Web page.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
-2

chrome.tabs.query() function returns tabs Array.

refer to this document: https://developer.chrome.com/extensions/tabs#method-query

when i tried tabs.query() function, the callback function returns all current tabS array.

chrome.tabs.query( {}, function(cbResult){
  console.log(cbResult);});

with google's document and above call back returns, you can find the proper tab is using first query() argument.

if the first argument is Null, then query function returns all tabs array. you can narrow the candidate tabs using the first argument.

Screenshot

Matt
  • 1,518
  • 4
  • 16
  • 30
  • 2
    the `chrome.tabs` api is not accessible inside a content script: https://stackoverflow.com/questions/15034859/chrome-tabs-returns-undefined-in-content-script – Jarrod Drysdale Jan 17 '19 at 18:52