Playground link: https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_sse
Problem: I am trying to override EventSource in such a way that there will be a console.log whenever onmessage triggers.
Code:
<!DOCTYPE html>
<html>
<body>
<h1>Getting server updates</h1>
<div id="result"></div>
<script>
if (typeof EventSource !== "undefined") {
const original = EventSource;
// part of chrome extension
window.EventSource = function EventSource(url) {
const ori = new original(url);
Object.getPrototypeOf(ori).onmessage = function (event) {
console.log(event); // log on every message
ori.onmessage(event);
};
return ori;
};
// actual source code
var source = new EventSource("demo_sse.php");
source.onmessage = function (event) {
document.getElementById("result").innerHTML += event.data + "<br>";
};
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
}
</script>
</body>
</html>
But I am getting this error:
Error:
VM461:10 Uncaught TypeError: Illegal invocation
at new EventSource (<anonymous>:10:42)
at <anonymous>:21:16
at submitTryit (tryit.asp?filename=tryhtml5_sse:853:17)
at HTMLButtonElement.onclick (tryit.asp?filename=tryhtml5_sse:755:133)
EventSource @ VM461:10
(anonymous) @ VM461:21
submitTryit @ tryit.asp?filename=tryhtml5_sse:853
onclick @ tryit.asp?filename=tryhtml5_sse:755
uic.js?v=1.0.5:1 Uncaught ReferenceError: adngin is not defined
at uic_r_p (uic.js?v=1.0.5:1:54492)
at HTMLButtonElement.onclick (tryit.asp?filename=tryhtml5_sse:755:148)
uic_r_p @ uic.js?v=1.0.5:1
onclick @ tryit.asp?filename=tryhtml5_sse:755
from "Uncaught TypeError: Illegal invocation" while overriding EventSource
No comments:
Post a Comment