Skip to content

Commit

Permalink
Merge pull request #14 from memeller/dev
Browse files Browse the repository at this point in the history
Autoresize, Optional Context Info, Bug Fixes
  • Loading branch information
memeller authored Dec 9, 2023
2 parents 80e8f2c + cbf3c3d commit b57e409
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 25 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog


## v5.7

- Added auto resize option for new messages. When enabled, each time an attachment is added / pasted inline, the extension checks if it meets the size restrictions set in the options. If so, the image is automatically resized and saved. (#12)
- Added an option to hide the extra info displayed in the attachments context menu when the file type is not supported (#13)
- Disabled Exif check on PNG files as it sometimes led to PNG files being rejected from resize
- Added 'tabs' to permissions - needed to tidy up the file map and detect the creation of new compose windows
- Added reloading of settings each time a compose window is opened

## v5.61

- Fixed wrong version in manifest (#10)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Each time a compatible (JPG/PNG/BMP) image is attached to/inserted into new e-ma

## New in this fork (vs darkrojan's version):

- Added Autoresize (currently works only for newly added attachments / inline images). This can be enabled for all images / inline only / attachments only. When enabled, each time an image is inserted / attached, the requirements for the resize function are checked (file size / dimensions, the ones set in options panel). If the image should be resized, it will be resized and replaced automatically, without any user interaction.
- Added PL lang support,
- BMP support (converted to JPG when resized),
- PNG support,
Expand Down
18 changes: 18 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@
"advanced.logenabled.label": {
"message": "Log messages to Error Console"
},
"advanced.contextInfo.label": {
"message": "Show message in context menu for unsupported types"
},
"advanced.autoResize.label": {
"message": "Auto resize images that meet size criteria"
},
"advanced.autoResize.off": {
"message": "Don't auto resize"
},
"advanced.autoResize.all": {
"message": "Auto resize all images"
},
"advanced.autoResize.attached": {
"message": "Auto resize attached images"
},
"advanced.autoResize.inline": {
"message": "Auto resize inline images"
},
"advanced.orient.label": {
"message": "Automatically rotate images"
},
Expand Down
18 changes: 18 additions & 0 deletions _locales/pl/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@
},
"advanced.logenabled.label": {
"message": "Loguj do Konsoli Błedów"
},
"advanced.contextInfo.label": {
"message": "Pokaż informację w menu kontekstowym o niewspieranym formacie pliku"
},
"advanced.autoResize.label": {
"message": "Automatycznie zmniejszaj obrazki spełniające kryteria"
},
"advanced.autoResize.off": {
"message": "Nie zmniejszaj automatycznie"
},
"advanced.autoResize.all": {
"message": "Automatycznie zmniejszaj wszystkie obrazki"
},
"advanced.autoResize.attached": {
"message": "Automatycznie zmniejszaj tylko załączniki"
},
"advanced.autoResize.inline": {
"message": "Automatycznie zmniejszaj tylko dodane w treści"
},
"advanced.orient.label": {
"message": "Automatycznie obróć obrazki"
Expand Down
24 changes: 20 additions & 4 deletions api/shrunked.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const resProto = Cc["@mozilla.org/network/protocol;1?name=resource"].getService(

let ready = false;
let logenabled = false;
let contextInfo = true;
var shrunked = class extends ExtensionCommon.ExtensionAPI {
getAPI(context) {
let { extension } = context;
Expand Down Expand Up @@ -141,7 +142,18 @@ var shrunked = class extends ExtensionCommon.ExtensionAPI {
indicies.push(i);
}
}
attachmentMenuItem.disabled = !indicies.length;
//check if a message should be displayed when accessing context menu on unsupported images
//if yes then set hidden to true, if not set it to disabled.
if(contextInfo)
{
attachmentMenuItem.disabled = !indicies.length;
attachmentMenuItem.hidden = false;
}
else
{
attachmentMenuItem.disabled = false;
attachmentMenuItem.hidden = !indicies.length;
}

if (!indicies.length) {
if (logenabled)
Expand Down Expand Up @@ -224,10 +236,12 @@ var shrunked = class extends ExtensionCommon.ExtensionAPI {
},
}).api(),
//I could not find any way of direct accessing storage.local from here. The 'recommended' way from topicbox is to use message send, but since i only need one boolean this should be enough.
setDebug(isEnabled) {
setOptions(isDebugEnabled,isContextInfoEnabled) {
let branch = Services.prefs.getBranch("extensions.shrunked.");
branch.setBoolPref("logenabled", isEnabled);
logenabled=isEnabled;
branch.setBoolPref("logenabled", isDebugEnabled);
branch.setBoolPref("contextInfo", isContextInfoEnabled);
contextInfo=isContextInfoEnabled;
logenabled=isDebugEnabled;
},
migrateSettings() {
let prefsToStore = { version: extension.version };
Expand All @@ -250,6 +264,8 @@ var shrunked = class extends ExtensionCommon.ExtensionAPI {
"options.resample": true,
"options.newalgorithm": true,
"options.logenabled": false,
"options.contextInfo": true,
"options.autoResize": "off",
resizeAttachmentsOnSend: false,
};

Expand Down
16 changes: 14 additions & 2 deletions api/shrunked.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
"parameters": []
},
{
"name": "setDebug",
"name": "setOptions",
"type": "function",
"async": true,
"parameters": [
{
"name": "isEnabled",
"name": "isDebugEnabled",
"type": "boolean"
},
{
"name": "isContextInfoEnabled",
"type": "boolean"
}
]
Expand Down Expand Up @@ -82,6 +86,14 @@
"logenabled":{
"type":"boolean",
"optional":true
},
"contextInfo":{
"type":"boolean",
"optional":true
},
"autoResize":{
"type":"string",
"optional":true
}
},
"optional": true
Expand Down
77 changes: 64 additions & 13 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ async function shouldResize(attachment, checkSize = true) {
let file = await browser.compose.getAttachmentFile(attachment.id);
return file.size >= fileSizeMinimum * 1024;
}
//check options on start and forward the logenabled setting to shrunked api
//get options and defaults from config
let options={}
let defaults={}
//check options on start and forward the logenabled/contextinfo setting to shrunked api
loadOptions().then((response)=>
{
logenabled=response.logenabled;
options=response.options;
defaults=response.defaults;
logenabled=response.options.logenabled;
contextInfo=response.options.contextInfo;
if(logenabled)
console.info("Shrunked Extension: Debug is enabled");
browser.shrunked.setDebug(logenabled);
browser.shrunked.setOptions(logenabled,contextInfo);
});
browser.shrunked.migrateSettings().then(prefsToStore => {
if (prefsToStore) {
Expand All @@ -35,8 +41,9 @@ browser.composeScripts.register({

browser.runtime.onMessage.addListener(async (message, sender, callback) => {
// Image added to body of message. Return a promise to the sender.
if (message.type == "resizeFile") {
return beginResize(sender.tab, message.file);
if (message.type == "resizeFile") {
//resizeFile is sent by content script when inline image is inserted. We need to tell the function that this is inline and that the action should trigger auto resize
return beginResize(sender.tab, message.file,false,true,true);
}
// Options window requesting a file.
if (message.type == "fetchFile") {
Expand All @@ -62,7 +69,8 @@ browser.compose.onAttachmentAdded.addListener(async (tab, attachment) => {
}

let file = await browser.compose.getAttachmentFile(attachment.id);
let destFile = await beginResize(tab, file);
//tell beginResize that this is not inline and that this event should trigger auto resize
let destFile = await beginResize(tab, file,true,false,true);
if (destFile === null || destFile === undefined) {
return;
}
Expand Down Expand Up @@ -153,14 +161,21 @@ browser.compose.onBeforeSend.addListener(async (tab, details) => {
});

// Get a promise that resolves when resizing is complete.
function beginResize(tab, file, notification = true) {
// For auto resize we need to know if the image isInline and if this is an event that should trigger auto resize. This method is called multiple times, not only when adding attachemnt
function beginResize(tab, file, notification = true,isInline=false,shouldResizeAuto=false) {
return new Promise((resolve, reject) => {
if (!tabMap.has(tab.id)) {
tabMap.set(tab.id, []);
}
let sourceFiles = tabMap.get(tab.id);
sourceFiles.push({ promise: { resolve, reject }, file });
if (notification) {
//check if autoResize is on and if current event should trigger it (shouldResizeAuto) and also if auto resize is turned on for this type of image (inline/attachment)
if(options.autoResize!="off" && shouldResizeAuto && ((options.autoResize=="inline" && isInline) || (options.autoResize=="attached" && !isInline) || options.autoResize=="all"))
{
doResize(tab.id, defaults.maxWidth,defaults.maxHeight,defaults.quality,file);
}
//if not proceed with notification
else if (notification) {
browser.shrunked.showNotification(tab, sourceFiles.length);
} else {
browser.shrunked.showNotification(tab, 0);
Expand Down Expand Up @@ -195,7 +210,9 @@ async function showOptionsDialog(tab) {
}

// Actual resize operation.
async function doResize(tabId, maxWidth, maxHeight, quality) {
// Since doResize usually tries to process all files, we need a special attribute (file) to tell it to process only one file when using auto resize.
// Without this each time a file is auto resized ALL of the attached files / inline images would be resized.
async function doResize(tabId, maxWidth, maxHeight, quality,file="") {
// Remove from tabMap immediately, then cancelResize will have nothing to do.
let sourceFiles = tabMap.get(tabId);
tabMap.delete(tabId);
Expand All @@ -207,9 +224,10 @@ async function doResize(tabId, maxWidth, maxHeight, quality) {
}
return;
}

let options=await loadOptions();

for (let source of sourceFiles) {
if(file!="" && source.file!=file)
continue;
let destFile = await browser.shrunked.resizeFile(
source.file,
maxWidth,
Expand All @@ -227,6 +245,7 @@ async function loadOptions(selectedOption=null)
{
options = await browser.storage.local.get({
"options.logenabled": false,
"options.contextInfo": true
});
options=options[selectedOption];
}
Expand All @@ -239,17 +258,33 @@ async function loadOptions(selectedOption=null)
"options.resample": true,
"options.newalgorithm": true,
"options.logenabled":false,
"options.contextInfo":true,
"options.autoResize":"off",
"default.maxWidth": 500,
"default.maxHeight": 500,
"default.quality": 75,
"default.saveDefault": true,
});
defaults = {
maxWidth: options['default.maxWidth'],
maxHeight: options['default.maxHeight'],
quality: options['default.quality'],
saveDefault: options['default.saveDefault'],
}
options = {
exif: options['options.exif'],
orientation: options['options.orientation'],
gps: options['options.gps'],
resample: options['options.resample'],
newalgorithm: options['options.newalgorithm'],
logenabled: options["options.logenabled"]
logenabled: options["options.logenabled"],
contextInfo: options["options.contextInfo"],
autoResize:options["options.autoResize"],
};

}
return options;

return {"options":options,"defaults":defaults};
}
function cancelResize(tabId) {
if (!tabMap.has(tabId)) {
Expand All @@ -266,6 +301,22 @@ function cancelResize(tabId) {
browser.tabs.onRemoved.addListener(tabId => {
tabMap.delete(tabId);
});
//reload settings each time a new compose window is opened
browser.tabs.onCreated.addListener(tab => {
if(tab.type=="messageCompose")
{
loadOptions().then((response)=>
{
options=response.options;
defaults=response.defaults;
logenabled=response.options.logenabled;
contextInfo=response.options.contextInfo;
if(logenabled)
console.info("Shrunked Extension: Debug is enabled");
browser.shrunked.setOptions(logenabled,contextInfo);
});
}
});
function changeExtensionIfNeeded(filename) {
let src = filename.toLowerCase();
//if it is a bmp we will save it as jpeg
Expand Down
13 changes: 13 additions & 0 deletions content/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ async function getAll() {
"options.resample": true,
"options.newalgorithm":true,
"options.logenabled":false,
"options.contextInfo":false,
"options.autoResize":"off",
resizeAttachmentsOnSend: false,
});

Expand Down Expand Up @@ -67,6 +69,8 @@ async function getAll() {
cb_orient.checked = prefs["options.orientation"];
cb_gps.checked = prefs["options.gps"];
cb_logenabled.checked=prefs["options.logenabled"]
cb_contextInfoEnabled.checked=prefs["options.contextInfo"]
s_autoResize.value=prefs["options.autoResize"]
// cb_logenabled.checked = prefs["log.enabled"];
s_resizeonsend.value = prefs.resizeAttachmentsOnSend;

Expand Down Expand Up @@ -106,6 +110,8 @@ addEventListener("load", async () => {

s_resizeonsend.addEventListener("change", setSendOption);
cb_logenabled.addEventListener("change", setCheckbox);
cb_contextInfoEnabled.addEventListener("change", setCheckbox);
s_autoResize.addEventListener("change", setAutoResizeOption);
browser.storage.onChanged.addListener(() => {
if (!settingFromThisPage) {
getAll();
Expand Down Expand Up @@ -179,3 +185,10 @@ async function setSendOption() {
});
settingFromThisPage = false;
}
async function setAutoResizeOption() {
settingFromThisPage = true;
await browser.storage.local.set({
"options.autoResize": s_autoResize.value,
});
settingFromThisPage = false;
}
11 changes: 11 additions & 0 deletions content/config.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@
</select>
</div>
<label><input type="checkbox" id="cb_logenabled" name="options.logenabled" /><span data-l10n-content="advanced.logenabled.label"></span></label>
<label><input type="checkbox" id="cb_contextInfoEnabled" name="options.contextInfo" /><span data-l10n-content="advanced.contextInfo.label"></span></label>
<div id="b_autoResize">
<label data-l10n-content="advanced.autoResize.label" />
<select id="s_autoResize">
<option value="off" data-l10n-label="advanced.autoResize.off" />
<option value="all" data-l10n-label="advanced.autoResize.all" />
<option value="attached" data-l10n-label="advanced.autoResize.attached" />
<option value="inline" data-l10n-label="advanced.autoResize.inline" />
</select>
</div>

</div>
</body>
</html>
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "__MSG_extensionName__",
"description": "__MSG_extensionDescription__",
"version": "5.62",
"version": "5.7",
"applications": {
"gecko": {
"id": "[email protected]",
Expand All @@ -20,7 +20,7 @@
"background": {
"scripts": ["background.js"]
},
"permissions": ["compose", "storage"],
"permissions": ["compose", "storage", "tabs"],
"options_ui": {
"page": "content/config.xhtml"
},
Expand Down
Loading

0 comments on commit b57e409

Please sign in to comment.