Created
September 26, 2023 13:45
-
-
Save TangoPJ/f02dac0f62231ebe7b2625c236991fd7 to your computer and use it in GitHub Desktop.
Editor.ts
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
// git repo: https://github.com/optimalux/js-agent/blob/master/src/editor.ts | |
export class Editor { | |
iframe: HTMLIFrameElement | |
destination: string | |
constructor(iframe: HTMLIFrameElement) { | |
this.iframe = iframe | |
this.destination = '*' | |
if (!this.iframe) { | |
console.warn('No iframe found') | |
} | |
} | |
connect(url: string): Promise<number> { | |
// TODO: call "reject" on timeout | |
return new Promise<number>((resolve) => { | |
window.addEventListener('message', (e) => { | |
console.log('message from site: ', e.data) | |
if (e.data === 'loaded') { | |
console.log('connected') | |
resolve(0) | |
} | |
}) | |
this.iframe.src = url | |
// Setting "destination" properly | |
const parsed_url = new URL(url) | |
this.destination = `${parsed_url.protocol}//${parsed_url.host}` | |
}) | |
} | |
replaceModification(id: string, css_selector: string, styles: string, new_content?: string) { | |
this.iframe.contentWindow?.postMessage( | |
{ | |
modification: [id, css_selector, styles, new_content], | |
}, | |
this.destination, | |
) | |
} | |
deleteModification(id: string) { | |
this.iframe.contentWindow?.postMessage( | |
{ | |
delete: id, | |
}, | |
this.destination, | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment