-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
225 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,54 @@ | ||
import { Spec } from './types'; | ||
|
||
top!.__vtbag ??= {}; | ||
top!.__vtbag.elementCrossing ??= {}; | ||
const elementCrossing = top!.__vtbag.elementCrossing!; | ||
|
||
console.log('[elc]', 'init'); | ||
if (top === self) { | ||
initBorderLands(); | ||
} else if (self.parent === top) { | ||
initHeartLand(); | ||
} else { | ||
console.log('[elc]', 'neither BorderLands nor HeartLand'); | ||
} | ||
const crossing = (top!.__vtbag.elementCrossing ??= { | ||
storageMap: new Map<string, Element>(), | ||
addrWeakMap: new WeakMap<any, string>(), | ||
fun: { | ||
pseudoAddress(object: object): string { | ||
let addr = '0x000000'; | ||
if (object === null || object === undefined) return addr; | ||
while (addr === '0x000000') { | ||
addr = | ||
crossing.addrWeakMap.get(object) ?? | ||
'0x' + Math.random().toString(16).slice(2, 8).toUpperCase(); | ||
} | ||
crossing.addrWeakMap.set(object, addr); | ||
return addr; | ||
}, | ||
setItem(id: string, element: Element) { | ||
crossing.storageMap!.set(id, element); | ||
}, | ||
getItem(id: string): Element | undefined { | ||
const element = crossing.storageMap!.get(id); | ||
return element; | ||
}, | ||
removeItem(id: string): void { | ||
crossing.storageMap!.delete(id); | ||
}, | ||
clear(): void { | ||
crossing.storageMap!.clear(); | ||
}, | ||
}, | ||
}); | ||
|
||
top === self && initBorderLands(); | ||
|
||
function initBorderLands() { | ||
console.log('[elc]', 'init BorderLands'); | ||
addEventListener('pagereveal', () => { | ||
console.log('[elc]', 'DOMContentLoaded'); | ||
top!.addEventListener('pagereveal', () => { | ||
const topDoc = top!.document; | ||
const root = topDoc.documentElement; | ||
root.innerHTML = `<body style="margin:0; overflow=clip"><iframe width=${innerWidth} height=${innerHeight} src="${location.href}"/>`; | ||
const lang = topDoc.documentElement.lang; | ||
const colorScheme = topDoc.documentElement.style.colorScheme; | ||
const root = topDoc.createElement('html'); | ||
root.innerHTML = `<body style="margin:0; overflow=clip"><iframe width=${innerWidth} height=${innerHeight} style="border:0" src="${location.href}"/>`; | ||
crossing.iframe = root.querySelector<HTMLIFrameElement>('iframe')!; | ||
root.lang = lang; | ||
root.style.overflow = 'clip'; | ||
root.style.colorScheme = colorScheme; | ||
topDoc.documentElement.replaceWith(root); | ||
crossing.frameDocument = topDoc.querySelector<HTMLIFrameElement>('iframe')!.contentDocument!; | ||
}); | ||
} | ||
|
||
function initHeartLand() { | ||
console.log('[elc]', 'init HeartLand'); | ||
self.addEventListener('pageswap', pageSwap, { once: true }); | ||
self.addEventListener('pagereveal', pageReveal, { once: true }); | ||
} | ||
|
||
function pageSwap() { | ||
console.log('[elc]', 'pageSwap'); | ||
|
||
self.document.querySelectorAll<HTMLElement>('[data-vtbag-x]').forEach((el) => { | ||
let id; | ||
const specs: Spec[] = []; | ||
el | ||
.getAttribute('data-vtbag-x') | ||
?.split(' ') | ||
.forEach((value) => { | ||
const [kind, key] = kindAndKey(value); | ||
switch (kind) { | ||
case 'id': | ||
id = key; | ||
break; | ||
case 'class': | ||
specs.push({ kind, key, value: el.classList.contains(key) ? 'true' : 'false' }); | ||
break; | ||
case 'style': | ||
specs.push({ | ||
kind, | ||
key, | ||
value: '' + (el.style[key as keyof CSSStyleDeclaration] ?? ''), | ||
}); | ||
break; | ||
case 'attr': | ||
specs.push({ kind, key, value: el.getAttribute(key) ?? '' }); | ||
break; | ||
default: | ||
console.error('[crossing]', 'unknown kind', kind); | ||
break; | ||
} | ||
}); | ||
top!.addEventListener('resize', () => { | ||
crossing.iframe!.width = '' + top!.innerWidth; | ||
crossing.iframe!.height = '' + top!.innerHeight; | ||
}); | ||
} | ||
|
||
function pageReveal() { | ||
console.log('[elc]', 'pageReveal'); | ||
const topDoc = top!.document; | ||
topDoc.title = document.title; | ||
top!.history.replaceState({}, '', location.href); | ||
} | ||
|
||
function kindAndKey(value: string) { | ||
let [kind, key] = value.split(':'); | ||
if (key === undefined) { | ||
key = kind.slice(1); | ||
switch (kind[0]) { | ||
case '#': | ||
kind = 'id'; | ||
break; | ||
case '.': | ||
kind = 'class'; | ||
break; | ||
case '@': | ||
kind = 'attr'; | ||
break; | ||
case '-': | ||
kind = 'style'; | ||
break; | ||
default: | ||
console.error( | ||
'[crossing]', | ||
'syntax error:', | ||
value, | ||
'is not recognized as a valid specification' | ||
); | ||
} | ||
} | ||
return [kind, key]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.