alienzhou / web-highlighter

✨ A no-runtime dependency lib for text highlighting & persistence on any website ✨🖍️
https://alienzhou.github.io/web-highlighter/
MIT License
888 stars 144 forks source link

Highlighting in iframe #77

Closed dattran2346 closed 3 years ago

dattran2346 commented 3 years ago

Currently I have a remote html document and I want to implement the auto highlighting feature on the remote html using web-highlighter. Below is my code

<iframe id="iframe-id" src="https://content.my-server.com/file.html">

// after the iframe have been loaded
let iframeDocument = document.getElementById("iframe-id").contentWindow.document
let highlighter = new Highlighter({
      $root: iframeDocument.documentElement,
      wrap: "span",
      style: {
        className: "highlight-class"
      }
    })

highlighter.run()

I have tested this code on the local document and it works great, however, the I can't do auto highlighting on remote resource.

alienzhou commented 3 years ago

Maybe due to some security policies about the iframe. Manipulating elements in the iframe from the parent seems to be dangerous for browsers.

Is there any error or warning on the console?

dattran2346 commented 3 years ago

It's is due to the CORS restriction. I can workaround it using Disable Cross-Origin Restrictions setting in Safari.