Badchip Posted September 4, 2021 Share Posted September 4, 2021 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 More sharing options...
Ingolme Posted September 4, 2021 Share Posted September 4, 2021 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. 1 Link to comment Share on other sites More sharing options...
Badchip Posted September 4, 2021 Author Share Posted September 4, 2021 So there is no other way to get the content from JS? Link to comment Share on other sites More sharing options...
Ingolme Posted September 4, 2021 Share Posted September 4, 2021 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. 1 Link to comment Share on other sites More sharing options...
Badchip Posted September 6, 2021 Author Share Posted September 6, 2021 (edited) I know some websites have CORS enabled but I'm not sure if this link have CORS too https://www.sony.com/en/top/2021/js/news.js Edited September 6, 2021 by Badchip Link to comment Share on other sites More sharing options...
Ingolme Posted September 6, 2021 Share Posted September 6, 2021 Looking at the HTTP headers, they are not permitting cross-origin requests on that file. 1 Link to comment Share on other sites More sharing options...
Badchip Posted September 15, 2021 Author Share Posted September 15, 2021 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 More sharing options...
Ingolme Posted September 15, 2021 Share Posted September 15, 2021 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> 1 Link to comment Share on other sites More sharing options...
Badchip Posted September 16, 2021 Author Share Posted September 16, 2021 (edited) 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 September 16, 2021 by Badchip Link to comment Share on other sites More sharing options...
Ingolme Posted September 16, 2021 Share Posted September 16, 2021 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> 1 Link to comment Share on other sites More sharing options...
_brym Posted September 21, 2021 Share Posted September 21, 2021 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 More sharing options...
Badchip Posted October 13, 2021 Author Share Posted October 13, 2021 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now