Search code examples
javascriptfirefoxyoutubefirefox-addonfirefox-addon-sdk

Making a add-on in Firefox to copy YouTube URL


I'm trying to make a firefox extension where, if a link to a youtube video is right-clicked, there is an option to copy the video URL in the context menu.

main.js

exports.main = function() {
    require("sdk/context-menu").Item({
        label: "Watch in MPC",
        context:  require("sdk/context-menu").SelectorContext("a[href]"), 
        contentScriptFile: require("sdk/self").data.url("check-node.js"),
        onMessage: function(msg){},
    });
};

check-node.js

self.on("context", function(node){
    if (node.href) return true;
});

How would i go about detecting that the link is indeed a youtube video link and then its URL?

I am using the Add-On Builder.


Solution

  • Something along the lines of the following should work, I guess:

    const ytl = /^https?:\/\/(?:youtu\.be\/|(?:www\.)?youtube\.com\/watch\?)/;
    self.on("context", function(node){
        return ytl.test(node.href);
    });