<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Climate…
- User
video.play().catch(function(err) { console.log('Autoplay was prevented:', err); }); // Play/Pause Button playPauseBtn.addEventListener('click', function() { playPauseBtn.src = video.paused ? "./assets/icons/pause.png" : "./assets/icons/play.png"; if (video.paused) { video.play(); } else { video.pause(); } }); // Mute/Unmute button muteUnmuteBtn.addEventListener('click', function() { video.muted = !video.muted; muteUnmuteBtn.src = video.muted ? "./assets/icons/mute.png" : "./assets/icons/unmute.png"; }); // Restart video from the beginning restartBtn.addEventListener('click', function() { video.currentTime = 0; video.play(); }); // Handle Subtitles subtitleBtn.addEventListener('click', function() { if (track.mode === 'hidden') { track.mode = 'showing'; subtitleBtn.style.opacity = 1; } else { track.mode = 'hidden'; subtitleBtn.style.opacity = 0.5; } subtitleBtn.src = track.mode === 'hidden' ? "./assets/icons/subtitle.png" : "./assets/icons/subtitles_selected.png"; }); //Handle Video Switch Based on language languageSelect.addEventListener('change', function() { var selectedLanguage = languageSelect.value; console.log("Selected Language is", selectedLanguage); video.src = videoDirectory[selectedLanguage].videoName; window.location.reload(); video.load(); video.play(); }); // const removeSubtitleTracks=()=> { // const subtitleTracks = video.getElementsByTagName('track'); // Object.keys(video.textTracks).map((e, i) => { // video.textTracks[e].mode = 'disabled' // }) // while (subtitleTracks.length > 0) { // video.removeChild(subtitleTracks[0]); // } // } // Handle captions language selection // languageSelect.addEventListener('change', function() { // var selectedLanguage = languageSelect.value; // // Hide all tracks // for (var i = 0; i < video.textTracks.length; i++) { // video.textTracks[i].mode = 'hidden'; // } // // Show the selected track // if (selectedLanguage !== 'off') { // for (var i = 0; i < video.textTracks.length; i++) { // if (video.textTracks[i].language === selectedLanguage) { // video.textTracks[i].mode = 'showing'; // } // } // } // }); // Initialize captions display // function initializeCaptions() { // var selectedLanguage = languageSelect.value; // for (var i = 0; i < video.textTracks.length; i++) { // if (video.textTracks[i].language === selectedLanguage) { // video.textTracks[i].mode = 'showing'; // } else { // video.textTracks[i].mode = 'hidden'; // } // } // } // Call initialize on page load // initializeCaptions(); // Fullscreen functionality fullscreenBtn.addEventListener('click', function() { if (!document.fullscreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) { enterFullscreen(videoContainer); } else { exitFullscreen(); } }); // Enter fullscreen function enterFullscreen(element) { if (element.requestFullscreen) { element.requestFullscreen(); } else if (element.webkitRequestFullscreen) { /* Safari */ element.webkitRequestFullscreen(); } else if (element.msRequestFullscreen) { /* IE11 */ element.msRequestFullscreen(); } fullscreenBtn.textContent = 'Exit Fullscreen'; } // Exit fullscreen function exitFullscreen() { if (document.exitFullscreen) { document.exitFullscreen(); } else if (document.webkitExitFullscreen) { /* Safari */ document.webkitExitFullscreen(); } else if (document.msExitFullscreen) { /* IE11 */ document.msExitFullscreen(); } fullscreenBtn.textContent = 'Fullscreen'; } // Update fullscreen button text on fullscreen change function onFullscreenChange() { if (!document.fullscreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) { fullscreenBtn.textContent = 'Fullscreen'; } else { fullscreenBtn.textContent = 'Exit Fullscreen'; } } // Add event listeners for fullscreen change document.addEventListener('fullscreenchange', onFullscreenChange); document.addEventListener('webkitfullscreenchange', onFullscreenChange); document.addEventListener('msfullscreenchange', onFullscreenChange);}); this is my script.js file and the error is : index.html:1 Unsafe attempt to load URL file:///Users/abhishek/Documents/showrooms/globePPTPlayer_test/assets/subtitles/english_subtitles.vtt from frame with URL file:///Users/abhishek/Documents/showrooms/globePPTPlayer_test/index.html. 'file:' URLs are treated as unique security origins. how to fix this
- Assistant
The error you're encountering:
Unsafe attempt to load URL file:///Users/abhishek/Documents/showrooms/globePPTPlayer_test/assets/subtitles/english_subtitles.vtt from frame with URL file:///Users/abhishek/Documents/showrooms/globePPTPlayer_test/index.html. 'file:' URLs are treated as unique security origins.is a common issue when trying to load local resources (like subtitles) directly from the
file://protocol in a browser. Modern browsers enforce strict Same-Origin Policy rules, which prevent webpages loaded viafile://from accessing other local resources due to security reasons.To resolve this issue, you should serve your project through a local web server instead of opening the HTML file directly in the browser. Here are several methods to set up a local server:
1. Using Python's Built-in HTTP Server
If you have Python installed, you can quickly start a local server with just a few commands.
For Python 3:
- Open your terminal or command prompt.
- Navigate to your project directory containing
index.html:cd /path/to/globePPTPlayer_test - Start the server:
This command starts a server on portpython3 -m http.server 80008000. - Open your browser and navigate to:
http://localhost:8000/index.html
For Python 2:
If you're using Python 2.x, use:
python -m SimpleHTTPServer 80002. Using Node.js
http-serverPackageIf you have Node.js installed, you can use the
http-serverpackage to serve your files.- Install
http-serverglobally:npm install -g http-server - Navigate to your project directory:
cd /path/to/globePPTPlayer_test - Start the server:
By default, it serves on porthttp-server8080. - Open your browser and go to:
http://localhost:8080/index.html
3. Using Visual Studio Code's Live Server Extension
If you use Visual Studio Code (VS Code), the Live Server extension provides an easy way to launch a local server.
-
Install Live Server:
- Open VS Code.
- Go to the Extensions view (
Ctrl+Shift+XorCmd+Shift+Xon Mac). - Search for Live Server by Ritwick Dey and install it.
-
Launch Live Server:
- Open your project folder in VS Code.
- Right-click on
index.htmlin the file explorer. - Select "Open with Live Server".
- Your default browser should open
index.htmlserved from a local server, typically on port5500.
4. Using Other Local Server Tools
There are various other tools and environments that can help you set up a local server, such as:
- XAMPP: A free and open-source cross-platform web server solution stack package.
- MAMP: A free, local server environment for macOS and Windows.
- WAMP: A Windows web development platform.
Additional Tips
-
Check File Paths: Ensure that your file paths are correct. For example, verify that
./assets/subtitles/english_subtitles.vttcorrectly points to the subtitle file relative toindex.html. -
CORS Issues: Serving files via a local server helps mitigate CORS (Cross-Origin Resource Sharing) issues, which are often the root cause of such errors.
-
Browser Caching: Sometimes, browsers cache resources. If you make changes and don't see them reflected, try clearing the cache or using an incognito/private window.
-
HTTPS Considerations: If you eventually deploy your project, consider serving it over HTTPS, especially if it involves sensitive data.
Example Using Python's HTTP Server
Here's a step-by-step example using Python:
- Open Terminal:
cd /Users/abhishek/Documents/showrooms/globePPTPlayer_test python3 -m http.server 8000 - Access in Browser:
Open
http://localhost:8000/index.htmlin your preferred browser.
This should load your
index.htmlwithout the security error, and your subtitles should work as expected.
By serving your project through a local web server, you align with the browser's security policies, allowing your HTML, JavaScript, and other assets to interact seamlessly.