I set up a webextension experiment with the following code in implementation.js and it worked:
var { ExtensionCommon } = ChromeUtils.import(“resource://gre/modules/ExtensionCommon.jsm”);
var { Services } = ChromeUtils.import(“resource://gre/modules/Services.jsm”);
var myapi = class extends ExtensionCommon.ExtensionAPI {
getAPI(context) {
return {
myapi: {
async YourFunctionName() {
let recentWindow = Services.wm.getMostRecentWindow("msgcompose");
if (recentWindow) {
try {
var editor_type = recentWindow.GetCurrentEditorType();
if (! (editor_type == "html" || editor_type == "htmlmail")) return;
var editor = recentWindow.GetCurrentEditor();
editor.beginTransaction();
var OldBody = editor.outputToString('text/html', 2);
var NewBody;
NewBody = OldBody.replace("ipsum", "<b>ipsum</b>"");
editor.rebuildDocumentFromSource(NewBody);
editor.endTransaction();
}
catch (e) {
// I don't know why, but console.log(e); did not work, therefore I used recentWindow.alert(e); for testing
console.log(e);
return false;
}
}
},
},
};
}
};