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

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

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

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

問(wèn)題描述

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

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

在本地開(kāi)發(fā),或者說(shuō)是在使用http://127.0.0.1:8088 或者 http://localhost:8088 本地調(diào)試時(shí),是沒(méi)有問(wèn)題的,但是如果使用綁定 host 或者使用不安全域(域名+http)時(shí),使用此功能,就會(huì)發(fā)生下面的報(bào)錯(cuò):

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

原因分析

安全問(wèn)題

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

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

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

問(wèn)題解決

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

document.execCommand

這里先說(shuō)一下 document.execCommand

寫(xiě)過(guò)原生 Editor 編輯器大家應(yīng)該都知道這個(gè)API吧,API 里面有很多方法,如:加粗/斜體/字號(hào)/字體顏色/插入圖片/插入鏈接/復(fù)制/剪切/撤銷(xiāo)… 具體可以看 MDN【但是這個(gè)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ù)制功能,請(qǐng)檢查更新或更換其他瀏覽器操作')
	}
}

完整代碼

 function copyToClipboard(textToCopy) {
  	// navigator clipboard 需要https等安全上下文
    if (navigator.clipboard && window.isSecureContext) {
        // navigator clipboard 向剪貼板寫(xiě)文本
        return navigator.clipboard.writeText(textToCopy);
    } else {
     	// document.execCommand('copy') 向剪貼板寫(xiě)文本
        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ù)制功能,請(qǐng)檢查更新或更換其他瀏覽器操作')
		}
    }
}

所有問(wèn)題都解決啦~

總結(jié)

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

相關(guān)文章

最新評(píng)論

昭觉县| 盘锦市| 黎川县| 镶黄旗| 康定县| 收藏| 肇庆市| 正定县| 胶州市| 武功县| 九龙坡区| 土默特左旗| 凤阳县| 公安县| 兴文县| 泸定县| 沙坪坝区| 宁夏| 方正县| 恩施市| 南溪县| 普安县| 合山市| 历史| 宿迁市| 应用必备| 威信县| 连州市| 普安县| 东平县| 彭州市| 濉溪县| 杭锦旗| 静宁县| 聊城市| 安远县| 新河县| 赤水市| 准格尔旗| 苍溪县| 赞皇县|