Skip to content

Instantly share code, notes, and snippets.

@prinsss
Last active September 14, 2023 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prinsss/02f9bdec3782495a1d76ba533f866f08 to your computer and use it in GitHub Desktop.
Save prinsss/02f9bdec3782495a1d76ba533f866f08 to your computer and use it in GitHub Desktop.
[User Script] Convert Gmail Quote Attribution to English
// ==UserScript==
// @name Convert Gmail Quote Attribution to English
// @namespace http://tampermonkey.net/
// @version 0.2.0
// @description Convert Gmail's attribution line for automatically quoted messages to format in English.
// @author prin
// @license MIT
// @match *://mail.google.com/*
// @match *://gmail.com/*
// @run-at context-menu
// @updateURL https://gist.github.com/prinsss/02f9bdec3782495a1d76ba533f866f08/raw/gmail-quote-english.user.js
// @downloadURL https://gist.github.com/prinsss/02f9bdec3782495a1d76ba533f866f08/raw/gmail-quote-english.user.js
// @homepage https://gist.github.com/prinsss/02f9bdec3782495a1d76ba533f866f08
// @grant none
// @require https://cdn.jsdelivr.net/npm/moment@2.29.4/moment.js
// @require https://cdn.jsdelivr.net/npm/moment@2.29.4/locale/zh-cn.js
// ==/UserScript==
(function () {
'use strict';
// By default, Gmail will quote all previous messages when you reply to an e-mail,
// and add an attribution line indicating following quoted content.
// Yup, it's a good practice to include a copy of original messages though,
// (FYI: https://en.wikipedia.org/wiki/Posting_style#Attribution_lines )
// the attribution line added by Gmail will differ in different locales.
// e.g. We will get something like this if you set your language to English,
// "On Mon, Dec 16, 2019 at 3:00 PM Amazon.co.jp <digital-no-reply@amazon.co.jp> wrote:"
// while on Chinese interfaces it will be like
// "Amazon.co.jp <digital-no-reply@amazon.co.jp> 于2019年12月16日周一 下午3:00写道:"
// which could be kinda odd in an English conversation.
// So here I wrote this script to help you transform the auto-generated
// attribution to English format. Hope this helps. ;)
// Updated on 2023/09/14, the current format is like this:
// zh_Hans: Pocket Support Team <support@getpocket.com> 于2023年9月14日周四 03:47写道:
// HTML: Pocket Support Team &lt;<a href="mailto:support@getpocket.com" target="_blank">support@getpocket.com</a>&gt; 于2023年9月14日周四 03:47写道:<br>
// en_US: On Thu, Sep 14, 2023 at 3:47 AM Pocket Support Team <support@getpocket.com> wrote:
function replaceQuoteAttr(str) {
const regex = /(.+?)\s*(&lt;|<)(.+@.+)(&gt;|>)\s*于\s*(.+)写道:/;
const parsed = regex.exec(str);
console.log({ regex, str, parsed });
if (!parsed) {
throw new Error(`Can't extract information from text:\n\n${str}`);
}
// 0: "Pocket Support Team <support@getpocket.com> 于2023年9月14日周四 03:47写道:"
// 1: "Pocket Support Team"
// 2: "<"
// 3: "support@getpocket.com"
// 4: ">"
// 5: "2023年9月14日周四 03:47"
const [match, sender, lt, address, gt, date] = parsed;
// Here we use U+202f "NARROW NO-BREAK SPACE" before AM/PM like Gmail does
// "2023年9月14日周四 03:47" => "Thu, Sep 14, 2023 at 3:47 AM"
const dateNewFormat = moment(date, 'YYYY[年]M[月]D[日]ddd HH:mm', 'zh-cn', true)
.locale('en')
.format('ddd, MMM D, YYYY [at] h:mm A');
// Use "<" or "&lt;" depends on the original text
const attrEnglish = `On ${dateNewFormat} ${sender} ${lt}${address}${gt} wrote:`;
console.log({ match, attrEnglish });
return { match, attrEnglish };
}
const ele = document.querySelector('div[role="textbox"] > .gmail_quote .gmail_attr');
if (!ele) {
const message =
'Can not find the attribution line.\n\n' +
'Please click the 3 dots to show trimmed quote, which contains attribution line in Chinese.\n\n' +
'Press "Cancel" to stop or "OK" to enter the text manually.';
if (window.confirm(message)) {
const text = window.prompt('Please enter the attribution text in Chinese:');
if (text) {
try {
const { attrEnglish } = replaceQuoteAttr(text);
window.alert(attrEnglish);
} catch (err) {
window.alert(err.message);
}
}
}
return;
}
try {
const { match, attrEnglish } = replaceQuoteAttr(ele.innerHTML);
if (window.confirm(`Would you like to replace\n\n"${match}" with\n\n"${attrEnglish}" ?`)) {
// Replace only first occurrence
ele.innerHTML = ele.innerHTML.replace(match, attrEnglish);
}
} catch (err) {
window.alert(err.message);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment