0

Hi I have a Chrome Extension that appends buttons to a Youtube page when the page loads. However, the buttons are not being appended even though I am using the jQuery append() function. The createButton() function also uses the append method. Is this a problem with the Chrome extension, or with how I am using jQuery?

Here is the code in question.

// On page load, add all buttons
function display() {
    var r = $('<div style="float: left"><input type="button" value=Add ></div>');
    r.click(function() {
        buttonAdd();
    });
    $("#watch-headline-title").append(r);
    var site = document.URL;
    timearray = JSON.parse(localStorage.getItem(site));
    if (timearray != null) {
        timearray = timearray.sort(compStr);
        for (i = 0; i < timearray.length; i++) {
            if (! $( "#" + timearray[i]).length ) {
                createButton(timearray[i]);
            }
        }
    }   
}

$( document ).ready(function() {
    display();
});
pcpetepete
  • 65
  • 2
  • 6
  • 1
    1. Is this code in a [content script](https://stackoverflow.com/q/4532236/)? 2. See [How to detect page navigation on Youtube and modify HTML before page is rendered?](http://stackoverflow.com/a/34100952) – wOxxOm Sep 04 '16 at 18:38
  • 2
    Can you give as some more context ? Is this code from a content script and if so, are you sure that content script gets injected into the desired website ? – Titus Sep 04 '16 at 18:38
  • 1
    Also. Are you sure that the `watch-headline-title` element exists when `$( document ).ready(....)` is called ? – Titus Sep 04 '16 at 18:39
  • Please [edit] the question to be on-topic: include a **complete** [mcve] that duplicates the problem. Usually, including a *manifest.json*, some of the background *and* content scripts. Questions seeking debugging help ("**why isn't this code working?**") must include: ►the desired behavior, ►a specific problem or error *and* ►the shortest code necessary to reproduce it **in the question itself**. Questions without a clear problem statement are not useful to other readers. See: "**How to create a [mcve]**", [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Sep 04 '16 at 18:51
  • Hi sorry, yes the code is run from a content script. Thanks for the link. I'll check it out. – pcpetepete Sep 04 '16 at 19:20

1 Answers1

1

It works!! I have to keep my .ready() there as well, but the link from @wOxxOm gave me the correct direction. Thank you so much. :)

How to detect page navigation on Youtube and modify HTML before page is rendered?

Community
  • 1
  • 1
pcpetepete
  • 65
  • 2
  • 6