Browser add-on that saves the current page as a HTML file with auto-redirect. It's like a bookmark you can store anywhere on your disk.
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.
 
webext_save-link-to-file/background.js

58 lines
1.3 KiB

/** HTML escape */
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
/** 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)
});
});