0
function getStyle(el, cssprop) {
  if (el.currentStyle) { // IE
    return el.currentStyle[cssprop];
  } else if (document.defaultView && document.defaultView.getComputedStyle) { // Firefox
    return document.defaultView.getComputedStyle(el, "")[cssprop];
  } else { // try and get inline style
    return el.style[cssprop];
  }
}

当被调用时它给出了这个错误它给出了这个错误

NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument arg 0 [nsIDOMWindow.getComputedStyle]
return document.defaultView.getComputedStyle(el, "")[cssprop];

所以函数调用是findOpacity(window.thirddiv,1). findOpacity来电getStyle。的代码findOpacity是这样的:

function findOpacity(node, minValue) {
    if(node==document.body) {
        return getStyle(document.body, 'opacity') < minValue
            ? getStyle(document.body, 'opacity')
            : minValue;
    } else {
        return findOpacity(node.parentNode, getStyle(node.parentNode, 'opacity'))
            < minValue
            ? findOpacity(node.parentNode, getStyle(node.parentNode, 'opacity'))
            : minValue;
    }
}
4

1 回答 1

1

我不知道究竟是什么导致你的问题,但试试

function findOpacity(node, maxValue) {
    var val = node===document.body
        ? getStyle(document.body, 'opacity')
        : findOpacity(node.parentNode);
    if(maxValue !== void(0)) val = Math.min(val, maxValue);
    return +val;
}

请注意,您应该避免类似的事情

getStyle(document.body, 'opacity') < minValue
    ? getStyle(document.body, 'opacity')
    : minValue;

因为你可以计算getStyle(document.body, 'opacity')两次。而且,minValue是最大值而不是最小值。

你的问题

问题在于它window.thirddiv不是一个 html 元素,而是一个XPC 包装器。而且您不能document.defaultView.getComputedStyle与 XPC 包装器一起使用。

我的猜测是您正在编写具有特权的 GreaseMonkey 脚本。然后,您应该阅读http://wiki.greasespot.net/XPCNativeWrappers,并获取真正的 html 元素,使用window.thirddiv.wrappedJSObject. 问题是您的代码很容易受到攻击,并且恶意脚本可以访问特权方法,例如GM_xmlhttpRequest.

如果您不创建 GM 脚本,则可以使用XPCNativeWrapper.unwrap(obj). 同样,这是一种不安全的做法。

于 2013-10-16T20:48:30.123 回答