Firefox 插件并不总是获取视频源

Firefox addon not always getting video src

我试图制作我的第一个插件,但我 运行 遇到了问题。我的插件是一个将图片和视频上传到网站的插件。图像工作正常,但对于视频,它有点偏离。这是我的代码:

var contextMenu = require("sdk/context-menu");
var tabs = require("sdk/tabs");

contextMenu.Item({
  label: "Upload to domain",
  context: contextMenu.SelectorContext("img,video"),
  contentScript:'self.on("click", function(node, data){self.postMessage(node.src);});',
  onMessage: function(imgSrc){
    tabs.open("http://domain.org/upload/?"+imgSrc);
  }
});

如果您播放右边的视频 here, the video url gets passed to the onMessage function fine. If you try it here,它不会通过 url。他们都是webms。我猜它只是不适用于 html5 播放器。我对么?有解决办法吗?

您查找来源的方法并不总是适用于 video 元素,因为与 img 不同,video 元素的来源可以指定为子元素 source是在第二种情况下发生的事情。要处理这种情况,您的代码必须类似于:

self.on("click", function(node, data){
    if(node.src){
        self.postMessage(node.src);
    }else if(node.nodeName.toUpperCase() === "video".toUpperCase()){
        var sources = node.children; // or may be node.querySelector("source");
        for(var i in sources){
            if(sources[i].src && sources[i].nodeName.toUpperCase() === "source".toUpperCase()){
                self.postMessage(node.src);
            }
        }
    }
}