feat: support for Web Worker env issue #124

This commit is contained in:
Zongmin Lei
2018-05-24 15:49:29 +08:00
parent 89d83019a9
commit 58d1140d20
6 changed files with 64 additions and 1 deletions

8
dist/xss.js vendored
View File

@@ -448,6 +448,14 @@ if (typeof window !== "undefined") {
window.filterXSS = module.exports;
}
// using `xss` on the WebWorker, output `filterXSS` to the globals
function isWorkerEnv() {
return typeof self !== 'undefined' && self instanceof DedicatedWorkerGlobalScope;
}
if (isWorkerEnv()) {
self.filterXSS = module.exports;
}
},{"./default":1,"./parser":3,"./xss":5}],3:[function(require,module,exports){
/**
* Simple HTML Parser

2
dist/xss.min.js vendored

File diff suppressed because one or more lines are too long

19
example/web.html Normal file
View File

@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Test xss.js</title>
</head>
<body>
</body>
<script src="../dist//xss.js"></script>
<script>
document.querySelector('body').innerText = filterXSS('<a href="#" onclick="alert(/xss/)">click me</a>');
</script>
</html>

25
example/webworker.html Normal file
View File

@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Test xss.js</title>
</head>
<body>
</body>
<script>
if (window.Worker) {
var myWorker = new Worker('worker.js');
myWorker.onmessage = function (e) {
document.querySelector('body').innerText = e.data;
};
} else {
alert('Your browser does not support Worker');
}
</script>
</html>

3
example/worker.js Normal file
View File

@@ -0,0 +1,3 @@
importScripts("../dist/xss.js");
postMessage(filterXSS('<a href="#" onclick="alert(/xss/)">click me</a>'));

View File

@@ -29,3 +29,11 @@ for (var i in parser) exports[i] = parser[i];
if (typeof window !== "undefined") {
window.filterXSS = module.exports;
}
// using `xss` on the WebWorker, output `filterXSS` to the globals
function isWorkerEnv() {
return typeof self !== 'undefined' && self instanceof DedicatedWorkerGlobalScope;
}
if (isWorkerEnv()) {
self.filterXSS = module.exports;
}