简体   繁体   English

如何在Firefox附加SDK中访问div?

[英]How to access div in Firefox Add-on SDK?

I'm trying to access the div that was right-clicked and log its code (from opening to closing tag) with Add-on SDK. 我正在尝试访问右键单击的div ,并使用附加SDK记录其代码(从开始标记到结束标记)。

var contextMenu = require("sdk/context-menu");
 var menuItem = contextMenu.Item({
 label: "Log This Div",
 context: contextMenu.SelectorContext("div"),
 contentScript: 'self.on("click", function (e) {' + // e is empty
                '  if (!e) {e = window.event;}' + // window doesnt have event property
                '  console.log(e);' + // result is {}
                '  var text = e.target;' +
                '  self.postMessage(text);' +
                '});',
 onMessage: function (selectionText) {
      console.log(selectionText); // null
 }
});

The first argument to the click callback is the actual context node , not an event. click回调的第一个参数是实际的上下文node ,而不是事件。

To get the outer markup of the node, you can use .outerHTML 要获取节点的外部标记,可以使用.outerHTML

var contextMenu = require("sdk/context-menu");
 var menuItem = contextMenu.Item({
 label: "Log This Div",
 context: contextMenu.SelectorContext("div"),
 contentScript: 'self.on("click", function (node, data) {' +
                '  self.postMessage(node.outerHTML);' +
                '});',
 onMessage: function (outerHTML) {
      console.log(outerHTML);
 }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM