Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getScrollBarSize.tsx 中的方法getTargetScrollBarSize在Firefox 52.9及其以前版本中报错“NS_ERROR_NOT_AVAILABLE” #261

Open
ppmmwozuiai opened this issue Jul 8, 2021 · 6 comments

Comments

@ppmmwozuiai
Copy link

2021.5.20的更新中加入的片段

环境:
windows xp 32位
firefox 52.9 esr 32位

Ref:

const { width, height } = getComputedStyle(target, '::-webkit-scrollbar');

function ensureSize(str: string) {
  const match = str.match(/^(.*)px$/);
  const value = Number(match?.[1]);
  return Number.isNaN(value) ? getScrollBarSize() : value;
}

export function getTargetScrollBarSize(target: HTMLElement) {
  if (typeof document === 'undefined' || !target) {
    return { width: 0, height: 0 };
  }

  const { width, height } = getComputedStyle(target, '::-webkit-scrollbar');  // 此行在Firefox中提示错误
  return {
    width: ensureSize(width),
    height: ensureSize(height),
  };
}

在firefox52.9(32位)及其以前版本(测试了51\47\46)会提示**"NS_ERROR_NOT_AVAILABLE"**的错误。
我想应该是getComputedStyle返回的CSSStyleDeclaration对象(浏览器中显示CSS2Properties)不可访问导致的。
如何修正这个问题?

@ppmmwozuiai
Copy link
Author

@zombieJ

@ppmmwozuiai
Copy link
Author

在较新的浏览器上CSSStyleDeclaration稳定地表现为对象,但是在Firefox较早版本里表现飘忽不定,表现更像是一个数组,但是在偶然情况下又表现为一个对象,具体在什么场景下表现为什么还没有找到规律

@shaodahong
Copy link
Member

image

看了一下 -webkit-scrollbar 是有兼容性的问题,但是时好时坏这个……

@jaryway
Copy link

jaryway commented Jul 16, 2021

+1

@jaryway
Copy link

jaryway commented Jul 16, 2021

win 10 firefox 52.0.2 下也有问题

@DeronW
Copy link

DeronW commented Sep 2, 2022

Firefox 60,遇到同样的问题。自己写了一个 Proxy,凑合用

const ua = navigator.userAgent;
const m = ua.match(/Firefox\/(\d+)/i);
if (m && parseInt(m[1]) <= 60) {
  const _getComputedStyle = getComputedStyle;

  const handler = {
    get: function (obj: any, prop: string) {
      try {
        return obj[prop];
      } catch (e) {
        console.log("catch getComputedStyle error", e);
        /**
         * 如果是 width 或者 height,需要返回字符串,因为 rc-util 直接对接过进行正则匹配,返回空会报错
         * https://github.com/react-component/util/blob/780a217fc67a15fc1f9170c107df001b21975f5e/src/getScrollBarSize.tsx#L57
         */
        if (prop == "width" || prop == "height") return "";
        else return undefined;
      }
    },
  };

  window.getComputedStyle = (
    elt: Element,
    pseudoElt?: string | null | undefined
  ): CSSStyleDeclaration => {
    return new Proxy(_getComputedStyle(elt, pseudoElt), handler);
  };
  console.log("Firefox 60 以下版本,替换 getComputedStyle 完成");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants