parent
87d5b332bb
commit
ae27f94866
1 changed files with 4 additions and 53 deletions
|
|
@ -16,61 +16,16 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
// Modified version derived from https://github.com/ali-raheem/Aify/blob/13ff87583bc520fb80f555ab90a90c5c9df797a7/plugin/content_scripts/compose.js
|
||||
|
||||
const makeParagraphs = (text, func) => {
|
||||
const chunks = text.split(/\n{2,}/);
|
||||
if (chunks.length == 1) {
|
||||
return func(document.createTextNode(text));
|
||||
}
|
||||
const paragraphs = chunks.map((t) => {
|
||||
const p = document.createElement("p");
|
||||
p.innerText = t;
|
||||
return p;
|
||||
});
|
||||
for (let i = paragraphs.length - 1; i >= 0; i--) {
|
||||
func(paragraphs[i]);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const insert = function (text) {
|
||||
const prefix = window.document.body.getElementsByClassName("moz-cite-prefix");
|
||||
if (prefix.length > 0) {
|
||||
const divider = prefix[0];
|
||||
let sibling = divider.previousSibling;
|
||||
while (sibling) {
|
||||
window.document.body.removeChild(sibling);
|
||||
sibling = divider.previousSibling;
|
||||
}
|
||||
}
|
||||
return makeParagraphs(text, function (p) {
|
||||
window.document.body.insertBefore(p, window.document.body.firstChild);
|
||||
});
|
||||
}
|
||||
|
||||
/*const insertReply = function (text) {
|
||||
const prefix = window.document.body.getElementsByClassName("moz-cite-prefix");
|
||||
if (prefix.length > 0) {
|
||||
const divider = prefix[0];
|
||||
let sibling = divider.previousSibling;
|
||||
return makeParagraphs(text, function (p) {
|
||||
window.document.body.insertBefore(p, sibling);
|
||||
});
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
browser.runtime.onMessage.addListener((message) => {
|
||||
browser.runtime.onMessage.addListener(async (message) => {
|
||||
switch (message.command) {
|
||||
case "getSelectedText":
|
||||
return Promise.resolve(window.getSelection().toString());
|
||||
|
||||
case "replaceSelectedText":
|
||||
case "replaceSelectedText": // TODO
|
||||
const selectedText = window.getSelection().toString();
|
||||
if (selectedText === '') {
|
||||
return insert(message.text);
|
||||
let fullBody = insert(message.text);
|
||||
await browser.compose.setComposeDetails(message.tabId, {body: fullBody.body.innerHTML});
|
||||
}
|
||||
const sel = window.getSelection();
|
||||
if (!sel || sel.type !== "Range" || !sel.rangeCount) {
|
||||
|
|
@ -99,10 +54,6 @@ switch (message.command) {
|
|||
case "getTextOnly":
|
||||
return Promise.resolve(window.document.body.innerText);
|
||||
|
||||
case "insertText":
|
||||
insert(message.text);
|
||||
break;
|
||||
|
||||
case 'promptTooLong':
|
||||
alert(browser.i18n.getMessage('msg_prompt_too_long'));
|
||||
break;
|
||||
|
|
|
|||
Loading…
Reference in a new issue