Skip to content

Instantly share code, notes, and snippets.

@mixedpuppy
Created March 30, 2018 15:23
Show Gist options
  • Save mixedpuppy/61bfc082fff115c851ea5449d2c6343f to your computer and use it in GitHub Desktop.
Save mixedpuppy/61bfc082fff115c851ea5449d2c6343f to your computer and use it in GitHub Desktop.
notes4-fix
--- background.js 2018-03-30 10:19:07.000000000 -0500
+++ background.js.fixed 2018-03-30 10:19:39.000000000 -0500
@@ -13,6 +13,8 @@
const timeouts = {};
let closeUI = null;
let isEditorReady = false;
+let editorConnectedDeferred;
+let isEditorConnected = new Promise(resolve => { editorConnectedDeferred = {resolve}; });
// Kinto sync and encryption
@@ -232,9 +234,11 @@
sendMetrics('open', {loaded: true});
closeUI = 'closeButton';
+ editorConnectedDeferred.resolve();
p.onDisconnect.addListener(() => {
// sidebar closed, therefore editor is not ready to receive any content
+ isEditorConnected = new Promise(resolve => { editorConnectedDeferred = {resolve}; });
isEditorReady = false;
sendMetrics('close', {'closeUI': closeUI});
});
@@ -269,24 +273,21 @@
browser.contextMenus.onClicked.addListener((info, tab) => {
// open sidebar which will trigger `isEditorReady`...
- browser.sidebarAction.open();
+ if (!isEditorReady) {
+ browser.sidebarAction.open();
+ }
// then send selection text to Editor.js once editor instance is initialized and ready
sendSelectionText(info.selectionText, tab.windowId);
});
// We receive this ... GREAT
-function sendSelectionText(selectionText, windowId) {
+async function sendSelectionText(selectionText, windowId) {
// if editor ready, go ahead and send selected text to be pasted in Notes,
// otherwise wait half a second before trying again
- if (isEditorReady) {
- chrome.runtime.sendMessage({
- action: 'send-to-notes',
- windowId,
- text: selectionText
- });
- } else {
- setTimeout(() => {
- sendSelectionText(selectionText, windowId);
- }, 500);
- }
+ await isEditorConnected;
+ chrome.runtime.sendMessage({
+ action: 'send-to-notes',
+ windowId,
+ text: selectionText
+ });
}
--- sidebar/app.js 2018-03-30 10:20:47.000000000 -0500
+++ sidebar/app.js.fixed 2018-03-30 10:21:13.000000000 -0500
@@ -28868,4 +28868,7 @@
/***/ })
/******/ ]);
-//# sourceMappingURL=app.js.map
\ No newline at end of file
+//# sourceMappingURL=app.js.map
+
+// Connect at the top level during load.
+browser.runtime.connect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment