Controlling an external web page through javascript code (not same-origin)

61 Views Asked by At

I am trying to let an user control an external webpage using his mic. Now im at that point, that accessing elements from that webpage from my javascript code is not allowed due to cross-origin policy. I understand the problem if i try to access that page, if its running in an iframe in my page. But is it possible to open a new tab and control that through mine ?

I have built the same functionality with python running on my pc, by using https://www.selenium.dev/documentation/webdriver/. This program opens a browser window and then I can get every element from the webpage, click buttons and input text. Why cant I do the same thing from my webpage? (I understand the cross-origin policy)

Are there any ideas how i can solve my problem or has somebody another idea how to solve that use case?

EDIT: app.jsx (not working due to same-origin policy)

function InsertNumber(number){
var iframe = document.getElementById("dartcounter");
var elmnt = 
iframe.contentWindow.document.getElementById("score");
console.log(elmnt)
//elmnt.innerHTML = "number";
}
export default function App() {
const [number, setNumber] = useState();

return (
    <div>
        <iframe id="dartcounter" src="https://dartcounter.net/en/game/match" width={1300} height={700}></iframe>
        <button onClick={() => InsertNumber(5)}>ID</button>
   </div >
)

main.py (running locally and working):

from selenium import webdriver
driver =webdriver.Chrome('/Users/danielusselmann/Downloads/chromedriver')
driver.get("https://www.dartcounter.net/de")
elem = driver.find_element(By.XPATH,'//button[contains(.,"Rückgängig")]')
elem.click()
1

There are 1 best solutions below

0
D.L On

In summary to the discussion above.

It is possible to use any back end language (python and nodejs) are two popular options.

The code is written in a script and run from the server side (in the simplest case, this could be your laptop or pc).

This would eliminate the need for a Webapp altogether.

Since the code in the question is written in javascript, you can usr nodejs here: https://nodejs.org

You can find the same webdriver for selenium here: https://www.npmjs.com/package/selenium-webdriver

And npm install like this (which is the equivalent to pythons pip install):

npm install selenium-webdriver