Skip to content

Script: CleanSubject

SamuelPlentz edited this page Mar 21, 2022 · 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:\s|r:\s|aw:\s|antw:\s|antwort:\s)/i, "");              // replace "Re: ", "R: ", "Aw: ", "Antw: ", "Antwort: "
  subject = subject.replace(/^(fwd:\s|fw:\s|wg:\s|wt:\s|wtrlt:\s)/i, "");                // replace "Fwd: ", "Fw: ", "Wg: ", "Wt: ", "Wtrlt: "
  subject = subject.replace(/^(Re-\d:\s|Re\[\d\]:\s)/i, "");                             // replace "Re-1: ", "Re-2: ", "Re[1]: ", "Re[2]: "
  subject = subject.replace(/^(\*\*\*\*SPAM\*\*\*\*\s|\[SPAM\]\s|\[SPAM\?\s\]\s)/i, ""); // replace "****Spam**** ", "[Spam] ", "[Spam? ] "
  subject = subject.replace(/^(Automatische Antwort:\s)/i, "");                          // replace "Automatische Antwort: "
  subject = subject.replace(/^(\[Extern]\s-\s|\[Extern]\s|\[Extern]:\s|Extern:\s)/i,""); // replace "[Extern] - ", "[Extern] ", "[Extern]: ", "Extern: "
  subject = subject.replace(/^(\[Probable Suspicious URLs\]\s)/i,"");                    // replace "[Probable Suspicious URLs] "
  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