Skip to content

Script: CleanSubject

SamuelPlentz edited this page Feb 17, 2020 · 8 revisions

Cleans the subject from multiple occurrences of subject prefixes such as "Fwd: Re:" or similar variants that were added in a conversation. (similar to the unfortunately outdated addon Clean Subject)

Script

// get subject from email if possible
if(!this.mWindow.document.getElementById('msgSubject')) return "";
let subject = this.mWindow.document.getElementById('msgSubject').value;


// detect first prefix
let firstPrefix = "";
if(subject.toLowerCase().startsWith("re: "))  firstPrefix = "Re: ";
if(subject.toLowerCase().startsWith("fwd: ")) firstPrefix = "Fwd: ";


// search and remove all prefixes
let previousLength = 0;
while(previousLength != subject.length) { // repeat the process if something gets shortened
  previousLength = subject.length;

  subject = subject.replace(/^(re: |r: |aw: |antw: |antwort: )/i, "");  // replace "Re: ", "R: ", "Aw: ", "Antw: ", "Antwort: "
  subject = subject.replace(/^(fwd: |fw: |wg: |wt: |wtrlt: )/i, "");    // replace "Fwd: ", "Fw: ", "Wg: ", "Wt: ", "Wtrlt: "
  subject = subject.replace(/^(Re-\d: |Re\[\d\]: )/i, "");              // replace "Re-1: ", "Re-2: ", "Re[1]: ", "Re[2]: "
  subject = subject.replace(/^(\*\*\*\*SPAM\*\*\*\* |\[SPAM\] )/i, ""); // replace "****Spam**** ", "[Spam] "
  subject = subject.replace(/^(Automatische Antwort: )/i, "");          // replace "Automatische Antwort: "
  subject = subject.trim();
}


// reuse first prefix and change subject
subject = firstPrefix + subject;
this.mWindow.document.getElementById('msgSubject').value = subject;
return "";

Usage

Using this script anywhere in your template would trigger the cleaning of the subject, it would not add anything to the email body itself:

[[SCRIPT=CleanSubject]]

Subject line before: Re: AW: Re: AW: Something funny

Subject line after: Re: Something funny

Clone this wiki locally