0

I'm trying to build a browser for extension for myself. The idea is that when I click the icon of the plugin, it opens a page. I then want to execute some code after that new page has finished loading but somehow it doesn't work.

var result;

chrome.browserAction.onClicked.addListener(function() {
    chrome.history.search(
        { text: "", maxResults: 100}, //object
        function(results) { //callback
            for(var item in results) {
                var currItem = results[item];
                if (currItem.url.indexOf("some_domain") > -1) {
                    result = results[item];
                    break;
                }
            }

            //Go to website
            chrome.tabs.create({
                'url': result.url
            }, function(tab) {
                new_tabId = tab.id;
            });
        }
    );
});

Now here comes the part that fails:

chrome.webNavigation.onCompleted.addListener(function(details) {
   // if (check for correct URL here) {
        var videos = document.getElementsByTagName("video");
        var video = videos[0];
        alert(videos.length); <--- always Zero! Why??
        video.load();
        video.play();

        video.addEventListener("ended", function() { ... });
   // }
});

They are both in the same background script and I do not have a content script. The permissions in the manifest are "tabs", "history", "webNavigation"

When I check with the developer console and do: document.getElementsByTagName("video").length I do get the correct number.

Gandora
  • 195
  • 1
  • 18

1 Answers1

1

As implied by wOxxOm, what will prevent your code from working is that you are attempting to access the DOM from a background script. Specifically, the code:

var videos = document.getElementsByTagName("video");
var video = videos[0];
alert(videos.length); <--- always Zero! Why??
video.load();
video.play();

video.addEventListener("ended", function() { ... });

will not function in a background script. If you want to do this you will need to load/execute a content script.

Community
  • 1
  • 1
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • Thanks @Mayken for holding down the fort with providing solutions on Stack while others are not so actively doing so (like myself). Really appreciate you! :) – Noitidart Aug 12 '16 at 16:11
  • Thanks to you and also especially to @wOxxOm - I will accept this answer (not like I have much choice :p). I was indeed able to make it work with a content script. Thanks again, the help is much appreciated :) – Gandora Aug 12 '16 at 19:17
  • 1
    @Gandora, I'm glad we were able to help. wOxxOm deserves the credit re. being first. My comment ? re. browser & ver. was due to my recently [answering a ?](http://stackoverflow.com/a/38910610/3773011) where one of the issues was, in FF48, `tabs.create()` calls its callback prior to the tab actually existing (and the `tabs.onCreated`, etc., events firing too soon). Thus, my ? re. browser & ver. based on just your ?'s title. Only after I actually read through code & text, did I see that attempting to access the DOM in a background script was clearly the problem (seconds after wOxxOm's comment). – Makyen Aug 12 '16 at 20:59
  • 1
    @Noitidart, Thanks and thanks for all the upvotes. I'm glad I've been able to cover the gap. I must admit, I've been wondering why the usual suspects :-) have not been answering lately. We all get busy with real life sometimes, or at least that's what I've been assuming has been the case. I blame :-) my involvement all on you for [giving me the accept on my first answer](http://stackoverflow.com/a/24398229/3773011) in a blatant attempt to lure me into answering questions on SO (not just blatant, but explicitly stated! :-) ). Thanks for that. I have enjoyed being helpful. – Makyen Aug 12 '16 at 21:14
  • Hahahaha no way!! Thanks so much for pointing out my accepting of your first answer! It's so great to hear that when you do something with just some hope, something that's not for sure, and it bares fruit. I really think you deserve it, your solutions are incredibly detailed. I've been busy with work lately and my uncle (unemployed) needed some help making a website/business. Really appreciate you, your solutions, your holding down the fort, and your thoughts! :) – Noitidart Aug 13 '16 at 02:58