Skip to content
This repository was archived by the owner on Dec 28, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions api/shrunked.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ var shrunked = class extends ExtensionCommon.ExtensionAPI {
let shouldShow = false;
if (target.nodeName == "IMG") {
console.log("Context menu on an <IMG>");
if (target.src.startsWith("data:image/jpeg;")) {
if (imageIsAccepted(target.src)) {
if (target.width > 500 || target.height > 500) {
shouldShow = true;
} else {
console.log("Not resizing - image is too small");
}
} else {
console.log("Not resizing - image is not JPEG");
console.log("Not resizing - image is not JPEG / PNG");
}
}

Expand Down Expand Up @@ -97,10 +97,8 @@ var shrunked = class extends ExtensionCommon.ExtensionAPI {
let reader = new FileReader();
reader.onloadend = function() {
let dataURL = reader.result;
dataURL =
"data:image/jpeg;filename=" +
encodeURIComponent(destFile.name) +
dataURL.substring(15);
let headerIndexEnd = dataURL.indexOf(";");
dataURL = reader.result.substring(0, headerIndexEnd) + ";filename=" + encodeURIComponent(destFile.name) + dataURL.substring(headerIndexEnd);
resolve(dataURL);
};
reader.readAsDataURL(destFile);
Expand Down Expand Up @@ -129,16 +127,15 @@ var shrunked = class extends ExtensionCommon.ExtensionAPI {
}
let attachment = items[i].attachment;
if (
attachment.url.startsWith("data:image/jpeg;") ||
/\.jpe?g$/i.test(attachment.url)
imageIsAccepted(attachment.url)
) {

indicies.push(i);
}
}

attachmentMenuItem.hidden = !indicies.length;
if (!indicies.length) {
console.log("Not resizing - no attachments were JPEG and large enough");
console.log("Not resizing - no attachments were JPEG/PNG and large enough");
} else if (indicies.length == 1) {
attachmentMenuItem.label = localeData.localizeMessage("context.single");
} else {
Expand Down Expand Up @@ -347,3 +344,10 @@ var shrunked = class extends ExtensionCommon.ExtensionAPI {
}
}
};

function imageIsAccepted(url) {
let src = url.toLowerCase();
let isJPEG = src.startsWith("data:image/jpeg") || src.endsWith(".jpg") || src.endsWith(".jpeg");
let isPNG = src.startsWith("data:image/png") || src.endsWith(".png");
return isJPEG | isPNG;
}
2 changes: 1 addition & 1 deletion background.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var tabMap = new Map();

async function shouldResize(attachment, checkSize = true) {
if (!attachment.name.toLowerCase().match(/\.jpe?g$/)) {
if (!attachment.name.toLowerCase().match(/((\.jpe?g)|(\.png))$/)) {
return false;
}
if (!checkSize) {
Expand Down
13 changes: 8 additions & 5 deletions compose_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ async function maybeResizeInline(target) {
console.log("Not resizing - image already has shrunked attribute");
return;
}
if (!imageIsJPEG(target)) {
console.log("Not resizing - image is not JPEG");
if (!imageIsAccepted(target)) {
console.log("Not resizing - image is not JPEG / PNG");
return;
}
if (target.width < 500 && target.height < 500) {
Expand Down Expand Up @@ -106,8 +106,9 @@ async function maybeResizeInline(target) {
let reader = new FileReader();
reader.onloadend = function() {
let dataURL = reader.result;
let headerIndexEnd = dataURL.indexOf(";");
dataURL =
"data:image/jpeg;filename=" + encodeURIComponent(destFile.name) + dataURL.substring(15);
reader.result.substring(0, headerIndexEnd) + ";filename=" + encodeURIComponent(destFile.name) + dataURL.substring(headerIndexEnd);
resolve(dataURL);
};
reader.readAsDataURL(destFile);
Expand All @@ -128,7 +129,9 @@ async function maybeResizeInline(target) {
}
}

function imageIsJPEG(image) {
function imageIsAccepted(image) {
let src = image.src.toLowerCase();
return src.startsWith("data:image/jpeg") || src.endsWith(".jpg");
let isJPEG = src.startsWith("data:image/jpeg") || src.endsWith(".jpg") || src.endsWith(".jpeg");
let isPNG = src.startsWith("data:image/png") || src.endsWith(".png");
return isJPEG | isPNG;
}
14 changes: 9 additions & 5 deletions modules/ShrunkedImage.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var XHTMLNS = "http://www.w3.org/1999/xhtml";
function ShrunkedImage(source, maxWidth, maxHeight, quality, options) {
this.maxWidth = maxWidth;
this.maxHeight = maxHeight;
this.imageFormat="image/jpeg";
this.quality = quality;
this.options = {
exif: true,
Expand Down Expand Up @@ -38,7 +39,7 @@ function ShrunkedImage(source, maxWidth, maxHeight, quality, options) {
if (match) {
this.basename = match[1];
} else {
match = /\/([\w.-]+\.jpg)$/i.exec(this.sourceURI.spec);
match =/\/([\w.-]+\.(jpe?g|png))$/i.exec(this.sourceURI.spec);
if (match) {
this.basename = match[1];
}
Expand All @@ -52,7 +53,10 @@ function ShrunkedImage(source, maxWidth, maxHeight, quality, options) {
this.sourceURI = Services.io.newURI(URL.createObjectURL(source));
this.basename = source.name;
}

if(this.basename.endsWith(".png"))
{
this.imageFormat="image/png";
}
if (!this.sourceURI) {
throw new Error("Unexpected source passed to ShrunkedImage");
}
Expand All @@ -75,7 +79,7 @@ ShrunkedImage.prototype = {
}

let blob = await this.getBytes(canvas);
return new File([blob], this.basename, { type: "image/jpeg" });
return new File([blob], this.basename, { type: this.imageFormat });
},
async readExifData() {
try {
Expand Down Expand Up @@ -186,8 +190,8 @@ ShrunkedImage.prototype = {
reject(ex);
}
},
"image/jpeg",
this.quality / 100
this.imageFormat,
(this.imageFormat=="image/jpeg")?(this.quality / 100):null
);
});
},
Expand Down