0

I'm trying to get my code to select the value assigned to a div box, and display it in an alert (more on this later). I was able to load the page, wait for the div to be assigned its value, and run the code no problem. However, when I had the code automatically execute when the tab was loaded by the extension, it fired before the div was assigned a value, and thus the command shell returned null (later, the alert box returned '---' which appears to be the default value of the div until it receives its value).

I'm looking to slow down the triggering of the content script before attempting to retrieve the div information, but have not been able to thus far.

Also, id like to be able to retrieve the value alertprice to the js file for later processing too, but Ive no idea where to begin with that.

pedit: New to JavaScript, and have read tutorials, but they've helped me little. I appreciate the method of breaking down the code segments and explaining the functionality for anyone that has the time

   function handleClick(state) {
  tabs.open({
    //url: a shopping/pricing website,
    onLoad: extract
  });
}

function extract(tab) {
  tab.off('onLoad',extract);
  tab.attach({
    contentScript: "var buyPrice = document.getElementById('buy-price');"+
    "var alertprice = buyPrice.innerHTML;"+
    "alert(alertprice);"
  });
}
Peligro
  • 23
  • 3
  • Couldn't you just wrap your code from `contentScript` into a `window.onload` function? Not your extensions code, but the code you inject. – schroffl Nov 09 '16 at 06:07
  • Delay methods for your `
    ` to have the content you want will be typical JavaScript delay methods for waiting for DOM changes (not extension specific, unless it is done resulting from network traffic, which you could monitor). See [this answer](http://stackoverflow.com/a/40418394/3773011) for a description of two options. Communication between content scripts and background scripts is described in [Content Scripts: Communicating with the add-on](https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts#Communicating_with_the_add-on).
    – Makyen Nov 09 '16 at 06:39
  • BTW: So far you have not mentioned wanting to do anything that can not be accomplished in a WebExtension. If you don't have things which require the Add-on SDK, you should consider using WebExtensions instead. Using it will gain you better forward compatibility, and cross browser compatibility. There are, however, things that can be done in the Add-on SDK, but not in a WebExtension. The [Introduction to Firefox add-ons in documentation](http://stackoverflow.com/documentation/firefox-addon/3235/introduction-to-firefox-add-ons/13574/introduction#t=201609290133319078047) may help. – Makyen Nov 09 '16 at 06:44
  • @schroffl, The content script is currently being attached when the `load` event fires (i.e. `onLoad: extract`). Wrapping the content script code in a `window.onload` event listener would result in the code never running because that event has already fired, and is unlikely to be fired a second time. – Makyen Nov 09 '16 at 07:04

0 Answers0