Skip to content
This repository was archived by the owner on Feb 22, 2021. It is now read-only.

Commit b4609b8

Browse files
committed
feat(pages): improve page loader
1 parent a3bf553 commit b4609b8

File tree

3 files changed

+17
-25
lines changed

3 files changed

+17
-25
lines changed

examples/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ document.title = 'hello'
44
const handler = async() => {
55
const backLoader = new BackLoader({
66
// scripts: ['https://code.jquery.com/jquery-3.2.1.slim.min.js'],
7-
// pages: ['https://google.com'],
8-
images: ['https://www.bing.com/az/hprichbg/rb/TadamiTrain_ROW14602114613_1920x1080.jpg'],
7+
pages: ['https://napos-care-plan.faas.ele.me/#/h5/board'],
8+
// images: ['https://www.bing.com/az/hprichbg/rb/TadamiTrain_ROW14602114613_1920x1080.jpg'],
99
})
1010
backLoader.start().on(event => {
1111
console.log(event)

src/core/loader.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EventHub } from './event'
22
import { LoaderEvent } from '../types'
3-
import { $fetch, filterResources, listenImageLoad } from '../utils/tools'
3+
import { $fetch, hiddenIframe, listenImageLoad } from '../utils/tools'
44

55

66
export class Loader {
@@ -22,16 +22,9 @@ export class Loader {
2222

2323
pages(urls: string[]): void {
2424
urls.forEach(url => {
25-
$fetch(url, { mode: 'no-cors' })
26-
.then(html => {
27-
this.scripts(filterResources(html, 'script'))
28-
this.styles(filterResources(html, 'style'))
29-
})
30-
.catch((e) => {
31-
this.emit(Object.assign({}, this.baseEvent, {
32-
type: 'page', source: url, success: false,
33-
}))
34-
})
25+
const iframe: HTMLIFrameElement = document.createElement('iframe')
26+
iframe.src = url
27+
document.body.appendChild(hiddenIframe(iframe))
3528
})
3629
}
3730

src/utils/tools.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,15 @@ export const filterResources = (source: string, type: string): string[] => {
3333
export const listenImageLoad = (images: HTMLImageElement[], done: (url: string) => void)
3434
: void => {
3535
images.forEach(img => img.onload = () => done(img.src))
36-
// const isCompleted = (imgs: HTMLImageElement[]) => !imgs.length
37-
// const timer: number = window.setInterval(() => {
38-
//
39-
// // images = images.map(img => {
40-
// // console.log(img)
41-
// // if (!img.complete) return img
42-
// // done(img.src)
43-
// // return null
44-
// // })
45-
// // .filter(v => !!v)
46-
// isCompleted(images) && clearInterval(timer)
47-
// }, 300)
36+
}
37+
38+
export const hiddenIframe = (iframe: HTMLIFrameElement): HTMLIFrameElement => {
39+
iframe.style.opacity = '0'
40+
iframe.style.position = 'fixed'
41+
iframe.style.top = '-20000px'
42+
iframe.style.left = '-20000px'
43+
iframe.style.zIndex = '-100'
44+
iframe.style.width = '1px'
45+
iframe.style.height = '1px'
46+
return iframe
4847
}

0 commit comments

Comments
 (0)