You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.3 KiB
59 lines
1.3 KiB
6 years ago
|
/** HTML escape */
|
||
|
function escapeHtml(unsafe) {
|
||
|
return unsafe
|
||
|
.replace(/&/g, "&")
|
||
|
.replace(/</g, "<")
|
||
|
.replace(/>/g, ">")
|
||
|
.replace(/"/g, """)
|
||
|
.replace(/'/g, "'");
|
||
|
}
|
||
|
|
||
|
/** Escape URL chars that could throw off the meta redirect tag */
|
||
|
function sanitizeUrl(unsafe) {
|
||
|
return unsafe
|
||
|
.replace(/;/g, "%3B");
|
||
|
}
|
||
|
|
||
|
/** Remove the most risky characters from a filename string */
|
||
|
function cleanFilename(unsafe) {
|
||
|
return unsafe
|
||
|
.replace(/[/\\?*|"'<>:]+/g, " ")
|
||
|
.replace(/\s+/g, " ");
|
||
|
}
|
||
|
|
||
|
browser.browserAction.onClicked.addListener((tab) => {
|
||
|
const escapedUrl = escapeHtml(sanitizeUrl(tab.url));
|
||
|
const escapedTitle = escapeHtml(tab.title);
|
||
|
const filename = cleanFilename(tab.title);
|
||
|
|
||
|
const content = new Blob([
|
||
|
`<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta charset="utf-8">
|
||
|
<title>${escapedTitle}</title>
|
||
|
<meta http-equiv="refresh" content="0; url=${escapedUrl}">
|
||
|
<style>
|
||
|
body {
|
||
|
padding: 1em;
|
||
|
text-align: center;
|
||
|
font-family: sans-serif;
|
||
|
color:#aaa;
|
||
|
}
|
||
|
a, a:visited, a:hover, a:link {
|
||
|
color:#888;
|
||
|
}
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
Redirecting to: <a href="${escapedUrl}">${escapedUrl}</a>
|
||
|
</body>
|
||
|
</html>
|
||
|
`]);
|
||
|
|
||
|
browser.downloads.download({
|
||
|
filename: `${filename}.link.html`,
|
||
|
url: URL.createObjectURL(content)
|
||
|
});
|
||
|
});
|