WebWorkerでは相対パスが効かない。
例えば /hoge/fuga/index.html から WebWorker を通じて ./data.txt を読み込もうとすると /data.txt を読み込もうとして、そんなファイルは無いと怒られてしまう。
あらかじめルートパスに変換してから WebWorker に渡してやると良い。
relpath2rootpath.js
function relpath2rootpath(relpath) {
if (/^https?:\/\//.test(relpath)) {
return relpath;
} else {
var rootpath = location.pathname;
rootpath = rootpath.substr(0, rootpath.lastIndexOf("/"));
rootpath = rootpath.split("/").filter(function(x) {
return x !== "";
});
relpath = relpath.split("/");
for (var i = 0; i < relpath.length; i++) {
if (relpath[i] === "..") {
rootpath.pop();
} else if (relpath[i] !== ".") {
rootpath.push(relpath[i]);
}
}
return "/" + rootpath.join("/");
}
}