(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:
- grab the link from the currentTab (which happens successfully)
- then I want to iterate through the other currentWindow tabs.
- I want to see which tab has the url "mywebsite.com".
- I then want to make "mywebsite.com" the activeTab,
- grab a DOM text field in the activeTab,
- add the url from step 1 into the DOM (text field) of step 4
Thank you for your help!