Skip to content

Instantly share code, notes, and snippets.

@TangoPJ
Created September 26, 2023 13:45
Show Gist options
  • Save TangoPJ/f02dac0f62231ebe7b2625c236991fd7 to your computer and use it in GitHub Desktop.
Save TangoPJ/f02dac0f62231ebe7b2625c236991fd7 to your computer and use it in GitHub Desktop.
Editor.ts
// 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