1

I am trying to open a new tab after getting the current tabid so that I can pass a message from the newly opened tab to the previous one, I am doing this:

myscr.js:

var myvar = 0;
chrome.tabs.query({
  currentWindow: true,
  active: true
}, function (tabs) {
  console.log(tabs[0]);
  myvar = tabs[0].id;
  //console.log(myvar);
});
var string_url = "getfilehandler.html/?" + "tabid=" + myvar;
window.open(string_url);

manifest.json:

"permissions": [
    "tabs",
    "contextMenus", "fileBrowserHandler"
],
"content_scripts": [
  {
    "matches": ["file:///home/user/Desktop/itproject/test.html"],
    "js": ["myscr.js"]
  }
],

My problem is when I am opening the file test.html the browser nothing seems to be happening!

the new window is opened when I am using just window.open(...) in myscr.js. I'm not really sure why it is happening! Any help would be greatly appreciated!

changes:

chrome.tabs.query({
    active: true,                              // Select active tabs
    windowId: chrome.windows.WINDOW_ID_CURRENT // In the current window
}, function(array_of_Tabs) {
    // Since there can only be one active tab in one active window, 
    //  the array has only one element
    var tab = array_of_Tabs[0];
    // Example:
    var url = tab.url;
console.log(url);
    // ... do something with url variable
});
  • 2
    The `chrome.tabs` API is not available to content scripts. Your code has to be moved to a background page if you really want to use the tabId. If you want to continue with using the tabId, make sure that you understand that the `chrome.tabs.query` method is asynchronous, as explained in [After calling chrome.tabs.query, the results are not available](http://stackoverflow.com/questions/11688171/after-calling-chrome-tabs-query-the-results-are-not-available/11689804#11689804). – Rob W Apr 20 '13 at 20:11
  • That code is quite unreadable. Consider editing your question to add the code. Since no-one has posted an answer yet, you can as well edit the existing question because it won't invalidate (non-existing) answers. – Rob W Apr 21 '13 at 16:05
  • @RobW: I have updated the changes in the question. I guess it's your code itself! managed to find it on some other thread! – Dwarakesh Pallagolla Apr 21 '13 at 16:07
  • Did you already move the code to the [background page](https://developer.chrome.com/extensions/background_pages.html)? Earlier I already said that the `chrome.tabs` API is unavailable to content scripts. You can trigger the code via the [`chrome.tabs.onUpdated`](https://developer.chrome.com/extensions/tabs.html#event-onUpdated) event, or send a message from the content script using the [message passing API](https://developer.chrome.com/extensions/messaging.html). – Rob W Apr 21 '13 at 16:10
  • oh! sorry I did not notice that (my bad)!! I want to modify the values of textfields of a specific webpage, so I have to stick with content scripts! do you know any other method where I can communicate between the tabs? – Dwarakesh Pallagolla Apr 21 '13 at 16:20
  • Yes, check out the documentation I linked in my previous comment. – Rob W Apr 21 '13 at 16:21

0 Answers0