Skip to content

Commit f943638

Browse files
author
Simon he
committed
chore: try catch
1 parent 4e0a68b commit f943638

File tree

11 files changed

+136
-76
lines changed

11 files changed

+136
-76
lines changed

src/addScript.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
export function addScript(src: string) {
2-
const s = document.createElement('script')
3-
s.type = 'text/javascript'
4-
s.async = true
5-
s.src = src
6-
const t = document.getElementsByTagName('script')[0]
7-
t.parentNode?.insertBefore(s, t)
2+
try {
3+
const s = document.createElement('script')
4+
s.type = 'text/javascript'
5+
s.async = true
6+
s.src = src
7+
const t = document.getElementsByTagName('script')[0]
8+
t.parentNode?.insertBefore(s, t)
9+
}
10+
catch (error: any) {
11+
throw new Error(error)
12+
}
813
}

src/addStyle.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
export function addStyle(s: string) {
2-
const style = document.createElement('style')
3-
style.innerHTML = s
4-
document.head.appendChild(style)
2+
try {
3+
const style = document.createElement('style')
4+
style.innerHTML = s
5+
document.head.appendChild(style)
6+
}
7+
catch (error: any) {
8+
throw new Error(error)
9+
}
510
}

src/copy.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
export function copy(s: string): boolean {
2-
const dom = document.createElement('textarea')
3-
dom.setAttribute('readonly', 'readonly')
4-
dom.value = s
5-
document.body.appendChild(dom)
6-
dom.select()
7-
const res = document.execCommand('copy')
8-
document.body.removeChild(dom)
9-
return res
2+
try {
3+
const dom = document.createElement('textarea')
4+
dom.setAttribute('readonly', 'readonly')
5+
dom.value = s
6+
document.body.appendChild(dom)
7+
dom.select()
8+
const res = document.execCommand('copy')
9+
document.body.removeChild(dom)
10+
return res
11+
}
12+
catch (error: any) {
13+
throw new Error(error)
14+
}
1015
}

src/download.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
export function download(url: string) {
2-
const a: HTMLAnchorElement = document.createElement('a')
3-
a.href = url
4-
a.download = url.substring(url.lastIndexOf('/') + 1, url.length)
5-
a.click()
2+
try {
3+
const a: HTMLAnchorElement = document.createElement('a')
4+
a.href = url
5+
a.download = url.substring(url.lastIndexOf('/') + 1, url.length)
6+
a.click()
7+
}
8+
catch (error: any) {
9+
throw new Error(error)
10+
}
611
}

src/exitFullscreen.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
export function exitFullscreen() {
2-
const el: any = (parent as any).documentElement
3-
const cfs = el?.cancelFullScreen || el?.webkitCancelFullScreen || el?.mozCancelFullScreen || el?.exitFullScreen
4-
if (cfs)
5-
cfs.call(el)
6-
else
7-
return new Error('切换失败,可尝试Esc退出')
2+
try {
3+
const el: any = (parent as any).documentElement
4+
const cfs = el?.cancelFullScreen || el?.webkitCancelFullScreen || el?.mozCancelFullScreen || el?.exitFullScreen
5+
if (cfs)
6+
cfs.call(el)
7+
else
8+
return new Error('切换失败,可尝试Esc退出')
9+
}
10+
catch (error: any) {
11+
throw new Error(error)
12+
}
813
}

src/fullScreen.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
export function fullScreen() {
2-
const el: any = document.documentElement
3-
const rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen
4-
if (rfs)
5-
rfs.call(el)
6-
else
7-
return new Error('浏览器不支持全屏')
2+
try {
3+
const el: any = document.documentElement
4+
const rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen
5+
if (rfs)
6+
rfs.call(el)
7+
else
8+
return new Error('浏览器不支持全屏')
9+
}
10+
catch (error: any) {
11+
throw new Error(error)
12+
}
813
}

src/isBottom.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
export function isBottom(distance = 0): boolean {
2-
return document.documentElement.clientHeight + window.scrollY + distance
3-
>= (document.documentElement.scrollHeight || document.documentElement.clientHeight)
2+
try {
3+
return document.documentElement.clientHeight + window.scrollY + distance
4+
>= (document.documentElement.scrollHeight || document.documentElement.clientHeight)
5+
}
6+
catch (error: any) {
7+
throw new Error(error)
8+
}
49
}

src/jsCookie.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
import type { JSCookie } from './types'
22
export const jsCookie: JSCookie = {
33
get(cname: string) {
4-
const name = `${cname}=`
5-
const ca = document.cookie.split(';')
6-
for (let i = 0; i < ca.length; i++) {
7-
const c = ca[i].trim()
8-
if (c.indexOf(name) === 0)
9-
return c.substring(name.length, c.length)
4+
try {
5+
const name = `${cname}=`
6+
const ca = document.cookie.split(';')
7+
for (let i = 0; i < ca.length; i++) {
8+
const c = ca[i].trim()
9+
if (c.indexOf(name) === 0)
10+
return c.substring(name.length, c.length)
11+
}
12+
return ''
13+
}
14+
catch (error: any) {
15+
throw new Error(error)
1016
}
11-
return ''
1217
},
1318
set(cname: string, cvalue: string, exdays?: any) {
14-
const d = new Date()
15-
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000))
16-
const expires = `expires=${d.toUTCString()}`
17-
document.cookie = `${cname}=${cvalue}; ${expires}`
19+
try {
20+
const d = new Date()
21+
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000))
22+
const expires = `expires=${d.toUTCString()}`
23+
document.cookie = `${cname}=${cvalue}; ${expires}`
24+
}
25+
catch (error: any) {
26+
throw new Error(error)
27+
}
1828
},
1929
remove(this: any, name: string) {
2030
this.set(name, '', -1)

src/lazyLoad.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
import { isArray } from './isArray'
22
import { isStr } from './isStr'
33
export function lazyLoad(imgList: any, root: Element, rootMargin = '0px 0px 200px 0px', threshold: any): void {
4-
if (isStr(imgList))
5-
imgList = document.querySelectorAll(imgList)
6-
if (imgList.length !== undefined)
7-
imgList = [...imgList]
8-
const observer = new IntersectionObserver(
9-
(entries, observer) => {
10-
entries.forEach((entry) => {
11-
/* 替换属性 */
12-
console.log(entry.isIntersecting)
13-
if (entry.isIntersecting) {
14-
(entry.target as HTMLImageElement).src = (entry.target as any).dataset.src
15-
observer.unobserve(entry.target)
16-
}
17-
})
18-
},
19-
{ rootMargin, root, threshold })
20-
if (isArray(imgList))
21-
(imgList as Element[]).forEach(img => observer.observe(img))
22-
else observer.observe(imgList as Element)
4+
try {
5+
if (isStr(imgList))
6+
imgList = document.querySelectorAll(imgList)
7+
if (imgList.length !== undefined)
8+
imgList = [...imgList]
9+
const observer = new IntersectionObserver(
10+
(entries, observer) => {
11+
entries.forEach((entry) => {
12+
/* 替换属性 */
13+
console.log(entry.isIntersecting)
14+
if (entry.isIntersecting) {
15+
(entry.target as HTMLImageElement).src = (entry.target as any).dataset.src
16+
observer.unobserve(entry.target)
17+
}
18+
})
19+
},
20+
{ rootMargin, root, threshold })
21+
if (isArray(imgList))
22+
(imgList as Element[]).forEach(img => observer.observe(img))
23+
else observer.observe(imgList as Element)
24+
}
25+
catch (error: any) {
26+
throw new Error(error)
27+
}
2328
}
2429

src/scrollToTop.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
export function scrollToTop() {
2-
const t = document.documentElement.scrollTop || document.body.scrollTop
3-
if (t > 0) {
4-
window.requestAnimationFrame(scrollToTop)
5-
window.scrollTo(0, t - t / 8)
2+
try {
3+
const t = document.documentElement.scrollTop || document.body.scrollTop
4+
if (t > 0) {
5+
window.requestAnimationFrame(scrollToTop)
6+
window.scrollTo(0, t - t / 8)
7+
}
8+
}
9+
catch (error: any) {
10+
throw new Error(error)
611
}
712
}

0 commit comments

Comments
 (0)