Web Worker is not working - javascript

I have written a very basic web worker, but is not working. Any help would be appreciated. Thanks.
Here's my HTML code:
<!DOCTYPE html>
<html>
<head>
<title>Basic Demo of Web Workers</title>
</head>
<body>
<button type="button" onclick="start()">Start!</button>
<button type="button" onclick="stop()">Stop!</button>
<output id="counterShow"></output>
</body>
<script>
var myWorker;
function start() {
if(window.Worker) {
myWorker = new Worker("http://yourjavascript.com/8257018521/basic-demo.js");
myWorker.onmessage = function(event) {
document.getElementById('counterShow').innerHTML = event.data;
};
myWorker.onerror = function(event) {
alert(event.message, event);
}
} else {
document.getElementsByTagName('BODY')[0].innerHTML = 'Sorry! Web workers are not supported.';
}
}
function stop() {
myWorker.terminate();
}
</script>
Here's the JS file that is hosted on a CDN (yourjavascript.com)
for(var i=0; i<100000; i++) {
postMessage(i);
}
The web worker is silently failing. Please help.

Uncaught DOMException: Failed to construct 'Worker': Script at 'http://yourjavascript.com/8257018521/basic-demo.js' cannot be accessed from origin 'null'
I believe this is due to security reason.
More info: Cross Domain Web Workers

100% a CORS issue. You cannot load web workers in the same way as normal scripts in <script> tags. Only if remote server allows alien host origins to load their files via ajax. Not sure why, but that's the way it is. See this workaround:
https://stackoverflow.com/a/33432215/607407

Well, I fixed it myself. I started a new project on CodePen and ran the code there, and it worked out pretty sweetly. I think the key requirement for Web Workers to run is to run them online, not locally.

Related

Data URL web worker loses security context

I am creating a web worker using a data URL in a local html file. The file has a secure context but the worker does not. Specs say they should be the same. Have I messed up, is the spec wrong or chromium browsers? Here is code demonstrating the error.
<!DOCTYPE html><html><head><title>Worker Security Flaw?</title>
<meta charset="UTF-8"><script>
function main(){
out.textContent= 'window.isSecureContext= '+isSecureContext;
let worker= new Worker('data:text/javascript,onmessage= function(e){postMessage({flag:isSecureContext});};');
worker.onmessage= function(e){out.textContent+= ', worker.isSecureContext= '+e.data.flag;};
worker.postMessage({});
}
</script></head>
<body onload=main();><output id="out"></output></body></html>
There is a work around using blobs.
let blb=new Blob(['onmessage=function(e){postMessage({flag:isSecureContext});};'], {type: 'text/javascript'});
let worker=new Worker(URL.createObjectURL(blb));
This works as the spec calls out giving the worker the same security context as the opener.

Implementation of NFC on Android Chrome 81 not working

Im trying to read NFC tags from chrome 81 on andriod with the following code:
<html>
<head>
<title>NFC</title>
</head>
<body>
<button onclick="reader()">Scan</button>
<script>
function reader(){
const reader = new NDEFReader();
reader.scan().then(() => {
alert("Scan started successfully.");
reader.onerror = () => {
alert("Cannot read data from the NFC tag. Try another one?");
};
reader.onreading = event => {
alert("NDEF message read.");
};
}).catch(error => {
alert(`Error! Scan failed to start: ${error}.`);
});
}
</script>
</body>
the problem im having with it is that it reads the entry from the nfc tag but doesnt give alerts like the code suggests, instead it trys to direct me to installed apps on my phone. However, when i use https://googlechrome.github.io/samples/web-nfc/ that is using the full API it works and displays in the webpage as data. The main difference is that im using the Enabling via chrome://flags method to allow the NFC API.
out of reading the tag, my only aim is to save the content to sessionStorage as a variable to be used by other parts of the website.
Thanks in advance
One difference between https://googlechrome.github.io/samples/web-nfc/ and your code that would matter is the fact this demo used to have an origin trial token in its web page.
For now, to experiment with Web NFC on Android, enable the #experimental-web-platform-features flag in chrome://flags as described in https://web.dev/nfc/#use
Hopefully this flag won't be required once it is shipped to the web platform.

How to run an EXE file through URL on .Net shared hosting

I try to run a simple ETL process on a schedule to populate a SQL Server database table on a .Net shared hosting. The EXE file will be hosted with the website and when it runs it will make some API calls and get data to update the website's SQL table.
My hosting company allows such thing (to call an exe file on schedule) with an extra fee, but they require me to have it wrapped and be called using URL. They don't mind any technology to use as long as I provide a URL. I did few attempt to get this setup working with no luck. For example I tried two ways below: Note, I just started to learn JavaScript, I use C# but this is my first time attempt to do something like that and I might be completely off. Any help will be appreciated.
<html>
<head>
<title>Open PMETL</title>
<script type="text/javascript">
function runProgram()
{
try {
var shell = new ActiveXObject("WScript.Shell");
var myPMETL="http://trudat.live/RefreshData.exe";
shell.Run(myPMETL);
}
catch (e) {
alert(e.message);
}
}
function runProgram02() {
if (window.ActiveXObject) {
try {
var excelApp = new ActiveXObject ("Excel.Application");
excelApp.Visible = true;
}
catch (e) {
alert (e.message);
}
}
else {
alert ("Your browser does not support this example.");
}
}
</script>
</head>
<body>
Run program
Run program02
</body>
</html>
I was able to achieve that by adding a new page to my ASP.NET application and port the console application into the code-behind C# and invoke the code through the Page_Load() method. This allowed my to give the hosting company a URL like http://mydomain/ExePage.aspx without impacting my original application since this new page is not reachable from the application's menu. it was a convenient way to give me what I needed.
I assume this is not a typical solution since I had the advantage to have the source code of the EXE program, but nevertheless it is a very effective, and this might help someone in the future.

HTML5 Web Speech API not working locally

I am trying to make this code work and don't know why is it not working locally. I tried the same on CodePen.io and it works.
<html>
<head>
<title>Voice API</title>
</head>
<body>
<button onClick="func()">Click Me</button>
<script>
function func()
{
alert('Hello');
var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onresult = function(event)
{
alert(event.results[0][0].transcript);
}
recognition.start();
}
</script>
</body>
Any suggestions?
You could try adding the following snippet to see what error is being generated.
recognition.onerror = function(event) {
console.log(event.error);
};
Chances are its spitting out a 'not-allowed' which generally means that the user agent is not allowing any speech input to occur for reasons of security, privacy or user preference (as you're running it locally through a file:// )
Have you tried serving the page under a local Web Server such as (IIS or Node) ?
Detailed discussion why camera (and microphone are not working on localhost here):
How to allow Chrome to access my camera on localhost?
In short, it is explicitly blocked.

Ping list of ip addresses using Javascript

I have a list of ip addresses to ping and would like to use Java script to do that.
I tried as the following but there is no output when I clicked the "Test" button.
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script>
function myFunction()
{
var myStringArray = [ "10.100.200.133", "10.10.22.0" ];
len=myStringArray.length
for (var i=0; len=myStringArray.length; i<len; i++) {
check=ping(i)
if $check Then
document.write("ip"+ i + "is up")
else
document.write("ip"+ i + "is down")
}
}
</script>
</head>
<body>
<button onclick="myFunction()">Test</button>
</body>
</html>
You can't directly "ping" in JavaScript. There may be a few other ways:
Ajax
Using a Java applet with isReachable
Writing a server-side script which pings, and using AJAX to communicate to your server-side script
You might also be able to ping in Flash (using ActionScript)
There are also some methods for pinging in JavaScript described in this question: Is it possible to ping a server from Javascript?
ping has a very specific meaning: https://www.rfc-editor.org/rfc/rfc1122#section-3.2.2.6, which cannot be replicated using a browser API.
If you want to just test if a web page hosted at a particular IP is available, you can use XMLHttpRequest

Categories

Resources