Q:

Current TabID for pageAction

I'm new in programming chrome extensions and this is/will be my first extension.
What I Want: For now I want to do a pageAction for a page, if a certain html-tag is shown. To do a pageAction it seems that I have to know the TabID of the current tab, so I tried this, which doesn't work (see comments in the code):
manifest.json (that works fine, just to show you what my manifest looks like)
{
    "manifest_version":2,

    "name":"ExtensionName",
    "description":"Description",
    "version":"1.0",

    "page_action":{
        "default_icon":"icon.png",
    },
    "content_scripts":[
        {
            "matches":[
                "http://www.domain.com/page.aspx"
            ],
            "js":["searchTag.js"],
            "run_at":"document_idle",
            "all_frames":false
        }
    ]
}

searchTag.js (the code is more or less like in answer of Arithmomaniac in how to get current tabId from background page)
if (document.getElementById("idIWant")) {
    var currentTab;
    alert(currentTab);  //this works and gives an alert with "undefined"
    //now the alert in the function callback doesn't work
    chrome.tabs.query(
        {currentWindow: true, active: true},
        function(tabArray) {
            alert(tabArray[0].id);
            currentTab = tabArray[0].id;
        }
    )
}

So what's wrong with my code? It seems that I did not use the chrome.tabs.query() correctly but I don't see it.

A:

searchTag.js is a content script (https://developer.chrome.com/extensions/content_scripts.html), which does not have access to chrome.tabs API. You have to send a message from the content script to the background page (https://developer.chrome.com/extensions/messaging.html), and call chrome.tabs.query in the background page. For example:
searchTag.js
if (document.getElementById("idIWant")) {
    chrome.runtime.sendMessage('showPageAction');
}

bg.js
var currentTab;

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  if (request == 'showPageAction') {
    chrome.tabs.query(
        {currentWindow: true, active: true},
        function(tabArray) {
            if (tabArray && tabArray[0])
                chrome.pageAction.show(tabArray[0].id);
        }
    );
    // sender will also contain the tab id so you can simply use
    // if (sender)
    //     chrome.pageAction.show(sender.tab.id);
  }
});

And add this to the manifest.json
"background": {
  "scripts": ["bg.js"],
  "persistent": true
},

