Youtube control video player using Javascript from FireFox add-on

I wrote a FireFox addon that has a toolbar button. In the click event, I try to find the EMBED element and try to call pauseVideo () on it, but it does not work.

I am sure that the object I receive is EMBED, so don't doubt it, because I show 'src'

var p1 = content.document.getElementById("movie_player");
window.alert(p1.src);

The pauseVideo () problem does not work:

try
{
p1.pauseVideo();
}
catch(e)
{
  window.alert(e); // this gives 'pauseVideo() is not a function'
}

In addition, the attribute 'allowcriptaccess' is set to 'always'. Any idea why this is not working? I have no ideas.

Thank!

+3
source share
2 answers

postMessage Youtube, - :

    var doc = content.document;
    var scpt = doc.getElementById("uniq_script_id");
    if (!scpt) {
        scpt = doc.createElement("script");
        scpt.type = "text/javascript";
        scpt.id = "uniq_script_id";
        var code = [
            'window.addEventListener("message", function(e) {',
            '  var cmd = e.data;',
            '  var player = document.getElementById("movie_player");',
            '  player[cmd]();',
            '}, false);'
        ].join('\n');
        scpt.textContent = code;
        doc.body.appendChild(scpt);
    }

    window.postMessage("pauseVideo", "*");
0

Source: https://habr.com/ru/post/1790714/


All Articles