Skip to content

TagSpaces is an offline, open source, document manager with tagging support

TS103 - Chrome saving files

Borislav Sapundzhiev6bbc7d29fd16
diff --git a/data/chromelight/popup.js b/data/chromelight/popup.js
index d65ce69f0..7ff9a030e 100644
--- a/data/chromelight/popup.js
+++ b/data/chromelight/popup.js
@@ -12,6 +12,7 @@
var html;
var htmlTemplate;
var tagLibrary;
+ var fileExt;
function init() {
$("#startTagSpaces").on("click", function(e) {
@@ -25,7 +26,9 @@
var tagList = extractAllTags(tagLibrary);
chrome.tabs.getSelected(null, function(tab) {
$('#title').val(tab.title);
- });
+ fileExt = getFileExt(tab.url);
+ $("#saveSelectionAsHtml").attr("disabled", (fileExt !== 'mhtml'));
+ });
$('#title').focus();
@@ -87,18 +90,30 @@
}
function saveAsMHTML() {
+ var filename;
+ tags = document.getElementById("tags").value;
+ if (tags) {
+ tags = tags.split(",").join(" ");
+ filename = $('#title').val() + ' [' + tags + '].' + fileExt;
+ } else {
+ filename = $('#title').val() + '.' + fileExt;
+ }
+
chrome.tabs.getSelected(null, function(tab) {
- tags = document.getElementById("tags").value;
- chrome.pageCapture.saveAsMHTML({
- tabId: tab.id
- }, function(mhtml) {
- if (tags) {
- tags = tags.split(",").join(" ");
- saveAs(mhtml, $('#title').val() + ' [' + tags + '].mhtml');
- } else {
- saveAs(mhtml, $('#title').val() + '.mhtml');
- }
- });
+
+ if(fileExt == 'mhtml') {
+ chrome.pageCapture.saveAsMHTML({
+ tabId: tab.id
+ }, function(mhtml) {
+ saveAs(mhtml, filename);
+ });
+ } else {
+ chrome.downloads.download({
+ url: tab.url,
+ filename: filename,
+ saveAs: true
+ });
+ }
});
}
@@ -208,6 +223,11 @@
return allTags;
}
+ function getFileExt(fileName) {
+ var ext = fileName.replace(/^.*?\.([a-zA-Z0-9]+)$/, "$1");
+ return (ext.length === fileName.length) ? 'mhtml' : ext;
+ }
+
$(document).ready(init);
}());
diff --git a/data/manifest.json b/data/manifest.json
index 46b9333df..fbd54f6ba 100644
--- a/data/manifest.json
+++ b/data/manifest.json
@@ -22,7 +22,8 @@
"<all_urls>",
"storage",
"pageCapture",
- "tabs"
+ "tabs",
+ "downloads"
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"homepage_url": "http://tagspaces.org"