最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于js復(fù)制內(nèi)容到瀏覽器剪貼板報錯:Cannot read properties of undefined (reading ‘writeText‘)的解決方案

 更新時間:2024年01月25日 08:38:41   作者:八了個戒  
這篇文章主要給大家介紹了關(guān)于js復(fù)制內(nèi)容到瀏覽器剪貼板報錯:Cannot read properties of undefined (reading ‘writeText‘)的解決方案,文中給出了詳細(xì)的原因分析和解決方案,需要的朋友可以參考下

如果想直接看解決方案,請下滑到最后【完整代碼】處

問題描述

開發(fā)「復(fù)制」功能

根據(jù)使用瀏覽器提供的原生功能 navigator.clipboard 返回的 Clipboard 對象的方法 writeText() 寫文本到剪貼板。

在本地開發(fā),或者說是在使用http://127.0.0.1:8088 或者 http://localhost:8088 本地調(diào)試時,是沒有問題的,但是如果使用綁定 host 或者使用不安全域(域名+http)時,使用此功能,就會發(fā)生下面的報錯:

Uncaught TypeError: Cannot read properties of undefined (reading 'writeText')

原因分析

安全問題

想必各位小伙伴或多或少會做過這樣的操作,復(fù)制一下自己的個人信息然后粘貼到其他地方,看是所謂的方便如果遇到惡意有毒的釣魚網(wǎng)站,那存在剪切板的信息就會一覽無余,網(wǎng)絡(luò)信息安全一直都是重中之重,所以在這方面上 navigator.clipboard 算是做足了防備。

SO~ 原因就是 瀏覽器禁用了非安全域的 navigator.clipboard 對象。

安全域包括本地訪問與開啟TLS安全認(rèn)證的地址,如 https 協(xié)議的地址、127.0.0.1 或 localhost 。

問題解決

所以要解決這個問題就是要做一個兼容的寫法,當(dāng)我們處于在安全域下使用 navigator.clipboard 提升效率,非安全域時退回到 document.execCommand('copy') 保證我們的復(fù)制功能一直可用。

document.execCommand

這里先說一下 document.execCommand

寫過原生 Editor 編輯器大家應(yīng)該都知道這個API吧,API 里面有很多方法,如:加粗/斜體/字號/字體顏色/插入圖片/插入鏈接/復(fù)制/剪切/撤銷… 具體可以看 MDN【但是這個API已經(jīng)被官方廢棄掉了】

「復(fù)制」功能示例:

function copy(text = ''){
	let input = document.createElement('input')
	input.style.position = 'fixed'
	input.style.top = '-10000px'
	input.style.zIndex = '-999'
	document.body.appendChild(input)
	input.value = text
	input.focus()
	input.select()
	try {
	  let result = document.execCommand('copy')
	  document.body.removeChild(input)
	  if (!result || result === 'unsuccessful') {
	    console.log('復(fù)制失敗')
	  } else {
	    console.log('復(fù)制成功')
	  }
	} catch (e) {
	  document.body.removeChild(input)
	  alert('當(dāng)前瀏覽器不支持復(fù)制功能,請檢查更新或更換其他瀏覽器操作')
	}
}

完整代碼

 function copyToClipboard(textToCopy) {
  	// navigator clipboard 需要https等安全上下文
    if (navigator.clipboard && window.isSecureContext) {
        // navigator clipboard 向剪貼板寫文本
        return navigator.clipboard.writeText(textToCopy);
    } else {
     	// document.execCommand('copy') 向剪貼板寫文本
        let input = document.createElement('input')
		input.style.position = 'fixed'
		input.style.top = '-10000px'
		input.style.zIndex = '-999'
		document.body.appendChild(input)
		input.value = textToCopy
		input.focus()
		input.select()
        try {
		  let result = document.execCommand('copy')
		  document.body.removeChild(input)
		  if (!result || result === 'unsuccessful') {
		    console.log('復(fù)制失敗')
		  } else {
		    console.log('復(fù)制成功')
		  }
		} catch (e) {
		  document.body.removeChild(input)
		  alert('當(dāng)前瀏覽器不支持復(fù)制功能,請檢查更新或更換其他瀏覽器操作')
		}
    }
}

所有問題都解決啦~

總結(jié)

以上就是關(guān)于js復(fù)制內(nèi)容到瀏覽器剪貼板報錯:Cannot read properties of undefined (reading ‘writeText‘)的解決方案的詳細(xì)內(nèi)容,更多關(guān)于js復(fù)制內(nèi)容到瀏覽器剪貼板報錯的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

天柱县| 磐石市| 中山市| 任丘市| 茶陵县| 乌鲁木齐县| 烟台市| 勐海县| 文水县| 乌海市| 荃湾区| 泊头市| 中卫市| 库尔勒市| 永州市| 遂川县| 平塘县| 武威市| 太原市| 沙湾县| 汉源县| 大英县| 东兰县| 古丈县| 盖州市| 罗平县| 贵阳市| 景德镇市| 安化县| 东阳市| 溧水县| 阿巴嘎旗| 故城县| 涟源市| 教育| 阿坝| 尉氏县| 鹿泉市| 师宗县| 阳泉市| 南召县|