gnuns / allOrigins

:alien: Pull content from any page as JSON via API

Home Page:https://allorigins.win/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

issues with re-writing hyperlinks

serapath opened this issue · comments

maybe it needs an additional option, but:

  1. using <iframe is="x-frame-bypass" src="https://api.allorigins.win/raw?url=${url}"></iframe>
  2. where url = "https://example.com/demo.html"
  3. and contains a relative hyperlink to e.g. /example/bar.html
  4. it is re-written to "https://api.allorigins.win/example/bar.html"
  5. but should probably be re-written to:
    • https://api.allorigins.win/raw?url=https://example.com/example/bar.html

Is that something that could be added?

just checked the source code - it's in fact not re-written at all, but instead that's just how the relative link works :-)

maybe something that would be useful to allow more freedom is to add postMessage interface to the iframe, like:

window.callbacks = {}
var counter = 0
onmessage = ({ data: code }) => {
  const id = counter++
  parent.postMessage(JSON.stringify({ type: 'start', id, data: code })
  new Promise((resolve, reject) => {
    window.callbacks[id] = { resolve, reject }
  }).then(result => {
    parent.postMessage(JSON.stringify({ type: 'result', id, data: result })
  }).catch(error => {
    error.stack
    parent.postMessage(JSON.stringify({ type: 'error', id, data: error })
  })
  const source = `;(async (id, result) {
    try {
      const { resolve } = window.callbacks[id];
      ${code};
      resolve(result)
    } catch (error) {
      const { reject } = window.callbacks[id]
      reject(error)
    }
  })(${id})`
  eval(source)
}

So a user can then scrape the page and interact properly with it.