Jump to content

Javascript equivalent to file_get_contents


Badchip

Recommended Posts

Based on this tutorial (https://lage.us/Javascript-File-Get-Contents.html) I can't get this to work with JS (like PHP) just get a blank screen:

function file_get_contents(filename) {
    fetch(filename).then((resp) => resp.text()).then(function(data) {
        return data;
    });
}
file_get_contents('https://www.sony.com/en/top/2021/js/news.js');

I have no problems with PHP:

<?php
$url = file_get_contents('https://www.sony.com/en/top/2021/js/news.js');
echo $url;

Any idea? thanks in advance.

Link to comment
Share on other sites

As with all AJAX-style APIs, with fetch() you're only permitted to load files from your own server unless the target server has granted permission to your website.

  • Like 1
Link to comment
Share on other sites

Not from other websites. Browsers implement this restriction for security purposes. Applications that want to work around the restriction will have a back-end script which fetches content from other websites and sends it to the front end.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Why can't I get the contents of this?

<script>
function file_get_contents(filename) {
    fetch(filename).then((resp) => resp.text()).then(function(data) {
        return data;
    });
}
file_get_contents('https://mediainfo.tf1.fr/mediainfocombo/L_LCI?context=MYTF1&pver=4014002');
</script>
Link to comment
Share on other sites

The code will need to be updated so that you are handling the data in the callback function. It actually cannot work identical to PHP's function because it's asynchronous.

This would work:

<script>
function file_get_contents(filename, callback) {
    fetch(filename).then((resp) => resp.text()).then(callback);
}

function handleResponse(data) {
  // Do something with the data here
  console.log(data);
}

file_get_contents('https://mediainfo.tf1.fr/mediainfocombo/L_LCI?context=MYTF1&pver=4014002', handleResponse);
</script>
  • Like 1
Link to comment
Share on other sites

I have replaced "// Do something with the data here" with "return data;" but I get a blank page.
What should I put to see the result?

By the way, can I send the content to a text file?

Thank you for your help.

Edited by Badchip
Link to comment
Share on other sites

You cannot return the data, you have to do something with it. Print it, manipulate it, store it into a variable or something else. In my previous example, I used console.log() to write the data to the Javascript console.

Javascript is not allowed to write to files, neither on the server nor on the user's computer, but you can prompt the user to download a generated text file like this:

<script>
function file_get_contents(filename, callback) {
    fetch(filename).then((resp) => resp.text()).then(callback);
}
function handleResponse(data) {
  
  // This code generates a text file with the data and asks the user to download it
  var blob = new Blob([data], { type: "text/plain;charset=utf-8" });
  var a = document.createElement("a");
  a.href = URL.createObjectURL(blob);
  a.download = "file.txt";
  document.body.appendChild(a);
  a.click();
  setTimeout(function() {
      document.body.removeChild(a);
      window.URL.revokeObjectURL(url);
  }, 0); 

}
file_get_contents('https://mediainfo.tf1.fr/mediainfocombo/L_LCI?context=MYTF1&pver=4014002', handleResponse);
</script>

 

 

  • Like 1
Link to comment
Share on other sites

The news.js file you pointed to wouldn't be of any use to you anyway. That file is what Sony use to load additional scripts conditionally.

A quick look at their sitemap gave me a link to their RSS feed. So I'd recommend you follow RSS tutorials here if you want to echo Sony's news feed. The RSS link is this: https://www.sony.com/en/SonyInfo/News/Press/data/pressrelease_for_top.xml

 

Link to comment
Share on other sites

  • 4 weeks later...

Now I'm trying to access a website with a cookie but I can't access the content.
Is this code correct?

<script>

var myInit = {
    cookie: 'SID=1234567890',
};

function file_get_contents(filename) {
    fetch(filename, myInit).then((resp) => resp.text()).then(function(data) {
	url = JSON.parse(data);
	location.replace(url['src'], myInit);
    });
}
file_get_contents('https://site.com/file.php')

</script>

Thank you again.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...