0

(ITERATION 2 COMES AFTER THIS PART. PLEASE SCROLL DOWN A BIT) I am trying to build an extension that would grab the URL of a youtube page (which is activeTab), and then it finds another tab with the title of 'Village', grabs an element in that DOM, and pastes the link.

I seem to be having trouble with my chrome.tabs.query({'title:}) or my getElementById from the not active tab.

Could someone please weigh in?

console.log('popupjs loads')

//get current URL of youtube or soundcloud
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
  currentURL = tabs[0].url;
  console.log(currentURL);
  //finds tab with village
  chrome.tabs.query({'title': 'Village'}, function(findVillage){
    //if there is a tab
    //if there is a current user
    //get link
    var myElement = document.getElementById("post-link"); 
    //create text with url of youtube or soundcloud
    var textnode = document.createTextNode(currentURL);
    //paste link
    myElement.append(textnode); <--- error happens here
    //press submit button
    //else
    //message error

  });
});

This is the error

Error in response to tabs.query: TypeError: Cannot read property 'append' of null
    at null.callback (chrome-extension://knmmfcephkakihklkejdofcijfpjgmmf/popping.js:17:14)
    at null.callback (chrome-extension://knmmfcephkakihklkejdofcijfpjgmmf/popping.js:9:15)

I understand that it says myElement does not have an object. I feel like there is a problem at the chrome tabs query for 'title'

Thank you!


----------ITERATION 2-------------------- I had a look at the link suggested in the comments, however, I am not having the same problem. I simply did not explain myself well enough.

I have refactored the code as best as I could, trying to make it more clear

popup.js

chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
  currentURL = tabs[0].url;
  console.log(currentURL);

  //iterate through tabs
  for (i=0; i <= chrome.tabs.length; i++) {
    //see if tab is mywebsit
    console.log(tabs[i].url);
    if (tabs[i].url == "mywebsite.com") {
      //if mywebsite, make that tab the active tab
      chrome.tabs.update(window.tabs[i].id, {active: true});
      //find the link-post text field, and paste in the youtube link
      document.getElementById("post-link").appendChild(currentURL);
    };
  };
});

I read through the links posted and through more docs. I am no longer getting the no object error. Instead I now get no error, but the code only makes it as far as the console.log(currentURL);. It doesn't log the console.log(tabs[i].url);

To iterate again: What I would like to do is:

  1. grab the link from the currentTab (which happens successfully)
  2. then I want to iterate through the other currentWindow tabs.
  3. I want to see which tab has the url "mywebsite.com".
  4. I then want to make "mywebsite.com" the activeTab,
  5. grab a DOM text field in the activeTab,
  6. add the url from step 1 into the DOM (text field) of step 4

Thank you for your help!

IWI
  • 1,528
  • 4
  • 27
  • 47
  • when referring to the document in the second callback, is that the document of the other tab, or the document of the currently selected tab? – bmartin Sep 06 '16 at 19:14
  • It's the document of the other tab. – IWI Sep 06 '16 at 21:08
  • 1
    The only thing I notice here is that append wont work with vanilla JS. appendChild will – bmartin Sep 06 '16 at 21:15
  • I do not think my question is the same as the link. In their problem, they were having trouble modifying the page rather than the extension. With my issue, I am able to access the chrome window. I have refactored some of my code and posted a clarification. Please see "SECOND ITERATION" on the top post. – IWI Sep 07 '16 at 17:35
  • Your code makes little sense. I highly recommend [Rubber Duck Debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging). You're trying to iterate through `chrome.tabs` - which isn't a collection of tabs but a collection of functions to work with them; then you access `tabs[i]`, when `tabs` is unchanged since it was set by `query` - and is at most 1 element array. Then, there's `window.tabs` for some reason. And finally, the duplicate pertains to your point 4 - if you want to change something in a tab, that's the correct dupe. – Xan Sep 08 '16 at 15:08

0 Answers0