Skip to content
Advertisement

How can i extract a value of a script variable from another website’s source code

Lets just say I am beginner here.

I have this script on one website::

<script>    
    var data;   
    var sn = "429d-d51aa-c2ec-df694-6a3gc"; 
    var videoPlayer;    
    var ea = "";    
    showCh("ba185497883dbfe02a43a400410e7960f073d475f3fb8949291de6bd18999cd77");    
.....
.....

and the exact same piece of code on another website/URL’s source code. I want to reference the value of variable sn and ShowCh value (the long alphanumeric string) in my code from the other website’s updating values. Sorry for my layman language and poor english

Advertisement

Answer

Since the other site almost certainly has CORS restrictions on it, you’ll have to bounce the request off your own server.

When your site loads, make a request to your backend. Have the backend do something like:

const fetch = require('node-fetch');
fetch('other-website-url')
  .then(res => res.text())
  .then((result) => {
    const sn = result.match(/ sn = "([^"]+)"/)[1];
    const chParam = result.match(/showCh*"(w+)/)[1];
    // Values retrieved; now send sn and chParam to client
  })
  .catch(handleErrors);

Then your website’s frontend can take the response from your server and populate the variables appropriately.

Advertisement