Skip to content

Commit

Permalink
Use webextension-polyfill for content message passing
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Feb 2, 2021
1 parent 23617a3 commit 2b9ddd4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
12 changes: 8 additions & 4 deletions src/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ browser.commands.onCommand.addListener((command) => {

browser.tabs
.query({ active: true, currentWindow: true })
.then((tabs) => {
browser.tabs.sendMessage(tabs[0].id, { action: "runPrettierFormat" });
})
.then((tabs) =>
Promise.all(
tabs.map((tab) =>
browser.tabs.sendMessage(tab.id, { action: "runPrettierFormat" })
)
)
)
.catch((err) => {
console.error("Error while querying for tabs", err);
console.error("Error occurred while sending message to tab.", err);
});
});
34 changes: 15 additions & 19 deletions src/content/extension.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* globals chrome */

import browser from "webextension-polyfill";
import prettier from "prettier/standalone";
import Storage from "./storage";
import { PARSERS, PARSERS_LANG_MAP } from "./parsers";
Expand All @@ -26,19 +25,16 @@ export default class Extension {
}

_bindListeners() {
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
browser.runtime.onMessage.addListener((message) => {
if (message.action !== "runPrettierFormat") {
return;
}

const inputEl = document.activeElement;

if (!inputEl?.tagName === "TEXTAREA") {
return;
}

this._format(inputEl);
inputEl.focus();
return Promise.resolve("Formatting complete!");
});
}

Expand All @@ -54,26 +50,26 @@ export default class Extension {

_format(inputEl) {
const options = this._storage.get().prettierOptions;

if (this._isStackOverflow()) {
this._formatStackOverflow(inputEl, options);
}

this._formatDefault(inputEl, options);
const inputValue = inputEl.value;
const formattedValue = this._isStackOverflow()
? this._formatStackOverflow(inputValue, options)
: this._formatDefault(inputValue, options);
inputEl.value = formattedValue;
inputEl.focus();
}

_formatDefault(inputEl) {
inputEl.value = prettier.format(inputEl.value, {
_formatDefault(inputValue) {
return prettier.format(inputValue, {
parser: "markdown",
plugins: PARSERS,
...this._storage.get().prettierOptions,
});
}

// https://stackoverflow.com/editing-help#code
_formatStackOverflow(inputEl) {
_formatStackOverflow(inputValue) {
let isInBlock = false;
const codeBlocks = inputEl.value.split("\n").reduce((groups, line) => {
const codeBlocks = inputValue.split("\n").reduce((groups, line) => {
const codeBlockRegex = /^\s{0,3}(?:```|~~~)/u;
const indentedCodeLangRegex = /^\s*<!-- language: lang-.* -->/u;
const langAllRegex = /^\s*<!-- language-all: lang-.+ -->/u;
Expand Down Expand Up @@ -250,11 +246,11 @@ export default class Extension {
}
}

inputEl.value = inputEl.value.replace(lines.join("\n"), formattedText);
return inputValue.replace(lines.join("\n"), formattedText);
});
}

inputEl.value = prettier.format(inputEl.value, {
return prettier.format(inputValue, {
parser: "markdown",
plugins: PARSERS,
...this._storage.get().prettierOptions,
Expand Down

0 comments on commit 2b9ddd4

Please sign in to comment.