Gmail的引用选择的文字的功能被灭了之后我参照其他大侠的脚本写了给自己用,最近因为浏览器的安全原因(防止DOM XSS),所以脚本不工作了,参照之前本站的帖子修复了一下,没问题,但突然发现只有回复,和回复所有工作,转发不工作了。。代码如下:
// ==UserScript==
// @name Quoted Selected Text In Gmail
// @version 2.4.0
// @description Re implement quoted select text function which disappeared from gmail Lab, you have to click reply/reply all to trigger this function, update on 04//11/2021 add support from menu click and support forward.
// @author Bill LIU
// @license MIT
// @updateURL https://bbs.ausmis.com/userscripts/QuoteSelectedTextInGmail.user.js
// @downloadURL https://bbs.ausmis.com/userscripts/QuoteSelectedTextInGmail.user.js
// @match *://mail.google.com/*
// @match *://gmail.com/*
// @grant none
// ==/UserScript==
document.addEventListener('click', function(e) {
if (e.target.dataset.tooltip != 'Reply to all' && //right top conner icon
e.target.dataset.tooltip != 'Reply' &&
e.target.textContent != 'Reply' && // right top conner dropdown menu item
e.target.textContent != 'Reply to all' &&
e.target.textContent != 'Forward' && //foward doesn't work so far
e.target.innerText != 'Reply all' && //buttom button
e.target.innerText != 'Reply' &&
e.target.innerText != 'Forward')
return;
insertSelectedTextintoEditor(e.target.textContent);
}, true);
window.addEventListener('keydown', e => {
console.log('Key pressed:', e.key);
// if press Ctrl、Alt、Meta key or press key is not 'r' and 'a',then return
if (e.ctrlKey ||
e.altKey ||
e.metaKey ||
( e.key != 'r' &&
e.key != 'a'))
return;
insertSelectedTextintoEditor('');
}, true);
function insertSelectedTextintoEditor(action)
{
var firstname;
var fullname;
//get the selected text
const sel = getSelection();
const selText = sel.toString().trim().replace(/[\r\n]/g, '<br>');
// if selected text is empty then quit
if (!selText)
return;
// get email
const item = sel.anchorNode.parentElement.closest('[role="listitem"]');
// if can't find the email then return
if (!item)
return;
// get sender's email,name and date of email
const emailNode = item.querySelector('[email]');
const email = emailNode && emailNode.getAttribute('email');
const date = (item.querySelector('span[title]') || {}).title || '';
//if forward then set name to Guys, otherwise get sender's name
if (action == 'Forward')
{
fullname = 'Guys';
}
else
{
fullname = emailNode && emailNode.getAttribute('name');
}
// if name has space then split, otherwise set to fullname
if (fullname.indexOf(' ') >= 0)
firstname = fullname.split(' ')[1];
else
{
firstname = fullname;
}
// debug output to console
/*
console.log('Selected text:', selText); // output selected text
console.log('Email:', email); // output email
console.log('Fullname:', fullname); // output fullname
console.log('Firstname:', firstname); // output firstname
console.log('Date:', date); // output date
*/
// try to get editor box, if can't find it then create a new one.
const getEditor = () => item.querySelector('.editable') || item.parentElement.querySelector('.editable');
Promise.resolve(getEditor() || new Promise(resolve =>
{
const t0 = performance.now();
const interval = setInterval(() =>
{
const editor = getEditor();
if (editor || performance.now() - t0 > 2000)
{
if (editor)
{
editor.closest('table').parentNode.closest('table').querySelector('[role="button"]').click();
//editor.textContent = '';
}
clearInterval(interval);
resolve(editor);
}
}, 100);
})).then(editor =>
{
if (!editor)
return;
editor.focus();
const escapeHTMLPolicy = window.trustedTypes.createPolicy("myEscapePolicy", {
createHTML: (string) => string,
});
const escaped = escapeHTMLPolicy.createHTML('Dear '+ firstname + ',<br><br>Please see my comments below:<br><div class="gmail_quote">' +
`<div style="background-color: rgb(221, 221, 221); color: rgb(102, 102, 102);"><i>` +
`${date} ${name == email ? '' : name + ' '}<<a href="${email}" target="_blank">${email}</a>></i> Wrote:</div>` +
`<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">${selText}</blockquote>` +
'</div><br><br>');
//make sure all quoted text are in beginning of editor area with default gmail style.
editor.firstChild.firstChild.insertAdjacentHTML('afterbegin', escaped);
});
}
初步估计是转发的时候editor的出现慢了??所以无法定位Editor,进而无法插入引用的文字??没研究明白,有没有大佬帮着看一下。。。