Created
November 7, 2018 14:11
-
-
Save Infocatcher/6c1bb9f9550fc935c8a5c86b645d3996 to your computer and use it in GitHub Desktop.
Validate URI in Firefox/Gecko, also see https://github.com/Infocatcher/Private_Tab/commit/028220e792ceb8d1e22d1bc06efc482b4fe3d09c (old method)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var uris = { | |
"http://example.com/": true, | |
"https://example.com/": true, | |
"https://example.com/foo/bar.php?key=val&key2=val2#hash": true, | |
"file:///": true, | |
"ftp://example.com": true, | |
//"about:": true, // Removed in new versions | |
"about:newtab": true, | |
"about:config": true, | |
"about:cache?device=memory": true, | |
"chrome://browser/content/browser.js": true, | |
"resource:///modules/DownloadsCommon.jsm": true, | |
"http://": false, | |
//"ftp://": false, | |
"http://?example.com/": false, | |
"http://#example.com/": false, | |
"http99://a/": false, | |
"unknown://": false, | |
"chrome://browser/": false, | |
"about:something-not-registered": false, | |
"not a URI": false, | |
"not-a-URI": false | |
}; | |
function isValidURI(spec) { | |
try { | |
Services.io.newURI(spec, null, null); // Forbid relative URIs | |
var req = new XMLHttpRequest(); | |
req.open("head", spec, true); // Simple way to try create nsIChannel instance | |
return !!req.channel; | |
} | |
catch(e) { | |
//Components.utils.reportError(e); | |
} | |
return false; | |
} | |
var failed = []; | |
for(var spec in uris) | |
if(isValidURI(spec) != uris[spec]) | |
failed.push(spec); | |
alert(failed.length ? "Test failed for\n" + failed.join("\n") : "OK!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment