Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions to remove text / tags from quoted parts #31

Open
RealRaven2000 opened this issue Jan 9, 2020 · 7 comments
Open

Add functions to remove text / tags from quoted parts #31

RealRaven2000 opened this issue Jan 9, 2020 · 7 comments
Assignees

Comments

@RealRaven2000
Copy link
Owner

@RealRaven2000 RealRaven2000 commented Jan 9, 2020

The functions %replaceText% and %deleteText% only work in the template and quote header, but not in the quoted email when replying. We need some functions to remove (or replace) text / html from quoted material.

I think it is also important to specify a minimum quote level starting with
0 = all quoted material
1 = work from the first email replied to and all contained quotes
2 = work from the "quoted quote" and all sub quotes
3 = work from the 3rd quote level onwards...
etc,

This way it could be used to remove images from older emails in a thread to conserve disk (and server) space.

Proposal for 4 new functions:

%deleteQuotedText(searchText,quoteLevel)%
%replaceQuotedText(searchText,replacementHTML,quoteLevel)%
%deleteQuotedTags(cssSelector,quoteLevel)%
%replaceQuotedTags(cssSelector,replacementHTML,quoteLevel)%

Examples:

1 - replace all images containing attribute moz-do-not-send=false from 2nd quote level onwards and replace with [SNIPPED]

%replaceQuotedTags("img[moz-do-not-send=false]","[SNIPPED]",2)%

2 - remove the sentence "Message received from external source." from quotes:

%deleteQuotedText("Message received from external source.",1)%

@RealRaven2000

This comment has been minimized.

Copy link
Owner Author

@RealRaven2000 RealRaven2000 commented Jan 10, 2020

Here is a first trial version which implements the variable %replaceQuotedText(searchText,replacementHTML,quoteLevel)%

Here is a test template:

%replaceQuotedText("replaceText","tralala")%
%replaceQuotedText("Message received","tralala")%
<hr>
<p>Dear %to(name)%</p>

important notes:

  • this replaces text from innerHTML, based on a regular expression match. white-space should be encoded as \s because there can be additional spaces / line breaks whenever there is a space between two words. Likewise special characters such as "." have to be escaped: "."
  • due to the pattern recognition of the parser for replaceQuotedText, the replacement string must be at least 1 character long, so to "remove" a string I recommend using a space character " " for now,

Example:
to remove the string "Message received from external source." use:

%replaceQuotedText("Message\s*received\s*from\s*external\s*source\."," ")%

smartTemplate-fx-2.8pre8.zip

To install, unzip the contained xpi file and drag into extensions manager. (github doesn't accept xpi in issues)

@RealRaven2000

This comment has been minimized.

Copy link
Owner Author

@RealRaven2000 RealRaven2000 commented Jan 10, 2020

OK, next I have implemented the function %deleteQuotedText()%. I also fixed a problem in the quote level parameter which didn't work correctly.

Example:
%deleteQuotedText("Message\s*received\s*from\s*external\s*source",2)%
This will remove the string from the second quote level (reply to reply) and lower. The second parameter is optional and defaults to 1 (affecting every quote).

For instructions on how to install / regular expressions use, please read the previous comment.

smartTemplate-fx-2.8pre11.zip

@RealRaven2000

This comment has been minimized.

Copy link
Owner Author

@RealRaven2000 RealRaven2000 commented Jan 10, 2020

OK, I added 2 more functions, just tested them. These are for removing tags (mainly images) from quoted mails:

%deleteQuotedTags(selector,quoteLevel)%
%replaceQuotedTags(selector,replacementHTML,quoteLevel)%

selector := what you would use to address the tag in css. simplest case "img", or "p" but attribute selectors can be added as long as double quotes can be avoided.
quoteLevel := which minimal quote level to start from, 2 should be a safe enough value as this will normally be content I have sent myself 2 mails ago (or older stuff)
replacementHTML := complete html code for a replacement tag, or simple text

Here is a nice example:

<style>.snipped { 
  border: 1px solid rgba(90,90,90,0.2); padding: 3px; 
  background: linear-gradient(to bottom, #fdeff4 0%,#fdb9bd 51%,#fea2a3 59%,#ff999e 100%);
}
</style>
%replaceQuotedTags("img","<span class='snipped'>snipped</span>",1)%

this will replace quoted images like this:
image

trial version:

smartTemplate-fx-2.8pre25.zip

(drag unzipped contents into Add-on manager to install)

@RealRaven2000

This comment has been minimized.

Copy link
Owner Author

@RealRaven2000 RealRaven2000 commented Jan 10, 2020

Quote level calculation was slightly off, here is a new improved version

  function quoteLevel(element, level) {
    if (!element || !element.parentNode)
      return level;
    let p = element.parentNode;
    if (p.tagName && p.tagName.toLowerCase()=="blockquote")
      return quoteLevel(p, level + 1); // increase level and check grandparent
    return quoteLevel(p, level);
  }

Fixed version:
smartTemplate-fx-2.8pre26.zip

@RealRaven2000

This comment has been minimized.

Copy link
Owner Author

@RealRaven2000 RealRaven2000 commented Jan 12, 2020

I added an additional parameter minSize for replacing / removing tags:

%deleteQuotedTags("selector",quoteLevel,minSize)%
%replaceQuotedTags("selector","replacementHTML",quoteLevel,minSize)%

selector - what you would use to address the tag in css. simplest case "img", or "p" but attribute selectors can be added as long as double quotes can be avoided.

replacementHTML - must be in double quotes - text or a html tag which will be inserted in plage.

quoteLevel - optional, (number) which minimal quote level to start from quoteLevel - for images I would recommend 2 if you want to remove images to retain some context in your replies.

minSize - (optional, numeric) - number of kByte, minimal size for removing the tag. This way you can keep smaller images like icons / emojis while discarding large images.

smartTemplate-fx-2.8pre44.zip

To Do: create a section in the "Variables" tab on the settings dialog

@AlexFinch

This comment has been minimized.

Copy link

@AlexFinch AlexFinch commented Jan 15, 2020

Thanks for that, I can stop polluting my replies, especially ones to mailing lists, with stupid text added by my employer.

@RealRaven2000

This comment has been minimized.

Copy link
Owner Author

@RealRaven2000 RealRaven2000 commented Jan 15, 2020

Thanks for that, I can stop polluting my replies, especially ones to mailing lists, with stupid text added by my employer.

Please make sure to test relentlessly.

A little problem with the minSize parameter (mostly relevant to images) is that apparently large images can be "not yet loaded" when composer opens (I see a class "loading-internal" and the src is abbreviated, so no valid size can be calculated). So I remove them anyway in that case in %replaceQuotedTags%. But I feel this should be handled by either a global setting or there should be some way to tell the variable in the parameters. Not quite sure how yet because I am very reluctant to add yet another parameter...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.