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

navigator.clipboard 兼容问题 #3372

Closed
4 tasks done
luocong2016 opened this issue Dec 1, 2023 · 10 comments
Closed
4 tasks done

navigator.clipboard 兼容问题 #3372

luocong2016 opened this issue Dec 1, 2023 · 10 comments

Comments

@luocong2016
Copy link
Contributor

luocong2016 commented Dec 1, 2023

⚠️ 重要 ⚠️ 在进一步操作之前,请检查下列选项。如果您忽视此模板或者没有提供关键信息,您的 Issue 将直接被关闭

  • 已阅读 文档.
  • 确保您的代码已是最新或者所报告的 Bug 在最新版本中可以重现. (部分 Bug 可能已经在最近的代码中修复)
  • 已在 Issues 中搜索了相关的关键词
  • 不是 ant design vue 组件库的 Bug

描述 Bug

navigator.clipboard 可能是 void 0

复现 Bug

/src/utils/copyTextToClipboard.ts 兼容问题

import { message } from 'ant-design-vue';

export function copyText(text: string, prompt: string | null = '已成功复制到剪切板!') {
  if (navigator.clipboard) {
    navigator.clipboard.writeText(text).then(
      function () {
        prompt && message.success(prompt);
      },
      function (error: Error) {
        message.error('复制失败!' + error.message);
      },
    );
    return;
  }
  if (document.execCommand) {
    const textArea = document.createElement('textarea');
    // textArea.style.display = 'none'; // 无法拷贝
    textArea.style.height = '0';
    textArea.style.padding = '0';
    textArea.value = text;
    textArea.setAttribute('readonly', 'readonly');
    document.body.appendChild(textArea);
    textArea.select();
    document.execCommand('copy');
    document.body.removeChild(textArea);
    return;
  }
  console.log(text);
}

系统信息

  • 操作系统:
  • Node 版本:
  • 包管理器 (npm/yarn/pnpm) 及其版本:
@luocong2016
Copy link
Contributor Author

luocong2016 commented Dec 1, 2023

设计的也有问题,无论成败都会提示去修改文件

详细描述下: 说设计有问题是,因为成功还是失败,都提示去改文件配置。并不是 Promise 的语法糖调用方式

@wangjue666
Copy link
Collaborator

设计的也有问题,无论成败都会提示去修改文件

什么浏览器环境下能复现呢?

@luocong2016
Copy link
Contributor Author

luocong2016 commented Dec 5, 2023

Chrome 已是最新版本
版本 119.0.6045.200(正式版本) (64 位)

浏览器兼容
image

问题依然存在,我猜测是浏览器设置的问题。但是我们不能强制用户去改配置。
所以,以下是我封装的 Promise 函数。

/**
 * 浏览器拷贝
 * @param content string
 * @returns Promise<void>
 */
export function copyText(content: string) {
  if (navigator.clipboard) {
    return navigator.clipboard.writeText(content);
  }
  if (Reflect.has(document, "execCommand")) {
    return new Promise<void>((resolve, reject) => {
      try {
        const textArea = document.createElement("textarea");
        textArea.value = content;
        // 在手机 Safari 浏览器中,点击复制按钮,整个页面会跳动一下
        textArea.style.width = "0";
        textArea.style.position = "fixed";
        textArea.style.left = "-999px";
        textArea.style.top = "10px";
        textArea.setAttribute("readonly", "readonly");
        document.body.appendChild(textArea);
        textArea.select();
        document.execCommand("copy");
        document.body.removeChild(textArea);
        resolve();
      } catch (e) {
        reject(e);
      }
    });
  }
  // throw Error('The previous methods were not compatible.');
  return Promise.reject('The previous methods were not compatible.');
}

@wangjue666
Copy link
Collaborator

image

之前我是想直接用这个包来处理的,但是vue3不兼容ie11,这个包也提示直接使用最新的api即可,就用navigator.clipboard来调用了

@wangjue666
Copy link
Collaborator

@luocong2016 意思你现在调用就是没有 navigator.clipboard这个对象吗?

@luocong2016
Copy link
Contributor Author

luocong2016 commented Dec 6, 2023

不是代码兼容问题,之前我就一直怀疑 的 chrome 权限问题,用户可能关闭了该权限。
我尝试了几次,发现开关 clipboard feature 权限,但是还是没有任何效果。

补充:
后面我是这么处理的

  1. 打开 chrome://flags,Reset all
  2. 再设置 Clipboard unsanitized read and write:Enabled
  3. 重启 chrome

我感觉后面版本的 chrome 默认关闭了很多权限,包括:蓝牙、摄像头等全部被关闭了。
这个可能是个坑。要不就要提示用户打开

@luocong2016
Copy link
Contributor Author

luocong2016 commented Dec 8, 2023

看ant-pro 设计的是这样的
image

后面测试了是可以的,
image

@wangjue666
Copy link
Collaborator

啊 加个try catch就能调用通了?

@luocong2016
Copy link
Contributor Author

不是啊, 人家设计的是 async function。 你设计的Promise,存在问题。前面已经提到了,不管是 then catch 都按finally 执行

@wangjue666
Copy link
Collaborator

不是啊, 人家设计的是 async function。 你设计的Promise,存在问题。前面已经提到了,不管是 then catch 都按finally 执行

ok, 那你提个PR吧,我来merge

luocong2016 added a commit to luocong2016/vue-vben-admin that referenced this issue Dec 12, 2023
zyt520ham pushed a commit to zyt520ham/vue-vben-admin that referenced this issue Dec 14, 2023
* main: (224 commits)
  fix:修复气泡确认框的底部按钮换行的问题 (vbenjs#3413)
  style: tabs bottom double line fix (vbenjs#3415)
  feat(IconPicker): IconPicker could allowClear and readonly for form (vbenjs#3414)
  fix(FormTable): Invert select bug (vbenjs#3412)
  chore: 自定义组件默认rule校验默认触发逻辑为blur
  chore(ApiRadioGroup): 移除多余的change事件
  fix(ApiSelect): 修复监听不到params的变动
  fix(BasicForm->ApiRadioGroup): when options click, duplicate requests. resolve vbenjs#3387
  Revert "feat: table搜索表单值发生改变可以触发reload (vbenjs#3378)" (vbenjs#3407)
  fix(BasicTable): ColumnSetting mistake when use setColumns (vbenjs#3408)
  fix: navigator.clipboard 兼容问题 vbenjs#3372 (vbenjs#3403)
  feat: ColumnSetting and SizeSetting persist (vbenjs#3398)
  fix(BasicDrawer): remove toRaw props (vbenjs#3399)
  feat(hooks): useWatermark添加水印防篡改功能(vbenjs#3395) (vbenjs#3397)
  fix: table height calc when fullcontent and footer visible change (vbenjs#3392)
  fix(BasicTable): table表格宽度自适应,隐藏的列导致宽度增加 (vbenjs#3388)
  fix: 使用suffix时,label没有垂直居中 (vbenjs#3384)
  feat: table搜索表单值发生改变可以触发reload (vbenjs#3378)
  feat(treeTable): add function collapseRows and demo (vbenjs#3375)
  perf(BasicTree): 外部获取treeData不必通过treeDataRef.value (vbenjs#3353)
  ...

# Conflicts:
#	internal/eslint-config/.eslintignore
#	internal/stylelint-config/.eslintignore
#	internal/vite-config/.eslintignore
#	package.json
#	pnpm-lock.yaml
#	src/locales/lang/en/sys.ts
#	src/locales/lang/zh-CN/sys.ts
#	src/store/modules/user.ts
#	src/views/demo/feat/session-timeout/index.vue
WitMiao pushed a commit to WitMiao/vue-vben-admin that referenced this issue Dec 17, 2023
zyt520ham pushed a commit to zyt520ham/vue-vben-admin that referenced this issue Dec 22, 2023
* gavin/v4-dev: (68 commits)
  feat: 组建修改
  fix:修复气泡确认框的底部按钮换行的问题 (vbenjs#3413)
  style: tabs bottom double line fix (vbenjs#3415)
  feat(IconPicker): IconPicker could allowClear and readonly for form (vbenjs#3414)
  fix(FormTable): Invert select bug (vbenjs#3412)
  chore: 自定义组件默认rule校验默认触发逻辑为blur
  chore(ApiRadioGroup): 移除多余的change事件
  fix(ApiSelect): 修复监听不到params的变动
  fix(BasicForm->ApiRadioGroup): when options click, duplicate requests. resolve vbenjs#3387
  Revert "feat: table搜索表单值发生改变可以触发reload (vbenjs#3378)" (vbenjs#3407)
  fix(BasicTable): ColumnSetting mistake when use setColumns (vbenjs#3408)
  fix: navigator.clipboard 兼容问题 vbenjs#3372 (vbenjs#3403)
  feat: ColumnSetting and SizeSetting persist (vbenjs#3398)
  fix(BasicDrawer): remove toRaw props (vbenjs#3399)
  feat(hooks): useWatermark添加水印防篡改功能(vbenjs#3395) (vbenjs#3397)
  fix: table height calc when fullcontent and footer visible change (vbenjs#3392)
  fix(BasicTable): table表格宽度自适应,隐藏的列导致宽度增加 (vbenjs#3388)
  fix: 使用suffix时,label没有垂直居中 (vbenjs#3384)
  feat: table搜索表单值发生改变可以触发reload (vbenjs#3378)
  feat(treeTable): add function collapseRows and demo (vbenjs#3375)
  ...

# Conflicts:
#	pnpm-lock.yaml
#	src/components/Application/src/AppProvider.vue
#	src/components/Container/src/ScrollContainer.vue
#	src/components/Drawer/src/BasicDrawer.vue
#	src/components/Form/src/BasicForm.vue
#	src/components/Form/src/componentMap.ts
#	src/components/Form/src/components/FormAction.vue
#	src/components/Form/src/hooks/useFormEvents.ts
#	src/components/Modal/src/BasicModal.vue
#	src/components/Modal/src/components/ModalWrapper.vue
#	src/components/Scrollbar/src/Scrollbar.vue
#	src/components/SimpleMenu/src/SimpleSubMenu.vue
#	src/hooks/web/usePermission.ts
#	src/layouts/default/content/index.vue
#	src/layouts/default/header/MultipleHeader.vue
#	src/layouts/default/menu/index.vue
#	src/layouts/default/tabs/components/TabContent.vue
#	src/layouts/default/tabs/index.vue
#	src/layouts/page/index.vue
#	src/locales/lang/en/common.json
#	src/locales/lang/en/sys.json
#	src/locales/lang/zh-CN/sys.json
#	src/router/guard/permissionGuard.ts
#	src/router/helper/routeHelper.ts
#	src/router/routes/basic.ts
#	src/router/routes/index.ts
#	src/router/routes/modules/about.ts
#	src/router/routes/modules/dashboard.ts
#	src/router/routes/modules/demo/charts.ts
#	src/router/routes/modules/demo/comp.ts
#	src/router/routes/modules/demo/feat.ts
#	src/router/routes/modules/demo/flow.ts
#	src/router/routes/modules/demo/iframe.ts
#	src/router/routes/modules/demo/level.ts
#	src/router/routes/modules/demo/page.ts
#	src/router/routes/modules/demo/permission.ts
#	src/router/routes/modules/demo/setup.ts
#	src/router/routes/modules/demo/system.ts
#	src/store/modules/multipleTab.ts
#	src/store/modules/permission.ts
#	src/store/modules/user.ts
#	src/utils/auth/index.ts
#	src/utils/env.ts
#	src/views/demo/form/UseForm.vue
#	src/views/demo/system/account/DeptTree.vue
#	src/views/sys/login/LoginForm.vue
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

2 participants