Skip to content

Commit

Permalink
Direct injection
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jun 30, 2024
1 parent 415f97b commit 0868e41
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
8 changes: 4 additions & 4 deletions source/advanced-editors-messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* @typedef { import('@codemirror/view').EditorView } EditorView
*/

// Do not write code outside this function, it won't be available because this function is serialized
export default function unsafeMessenger() {
function unsafeMessenger() {
const lastKnownValue = new WeakMap();
document.body.addEventListener('gt:get', listener);

Expand Down Expand Up @@ -129,5 +128,6 @@ export default function unsafeMessenger() {
}
}

// eslint-disable-next-line no-unused-expressions
undefined; // Avoids issues with tabs.injectScript
unsafeMessenger();

console.log('Advanced editors messenger injected!')
7 changes: 7 additions & 0 deletions source/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ async function handleAction({id}) {
target: {tabId: id, frameIds: virginFrames},
injectImmediately: true,
});

chrome.scripting.executeScript({
files: ['/advanced-editors-messenger.js'],
target: {tabId: id, frameIds: virginFrames},
world: 'MAIN',
injectImmediately: true,
});
}

function handlePortListenerErrors(listener) {
Expand Down
14 changes: 6 additions & 8 deletions source/ghost-text.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import GThumane from './humane-ghosttext.js';
import advancedEditors from './advanced-editors-messenger.js';
import optionsStorage from './options-storage.js';

const knownElements = new Map();
Expand Down Expand Up @@ -153,6 +152,12 @@ class GhostTextField {
return;
}

if (this.field.value === undefined) {
console.log('field value is undefined');
console.log(this.field);
return;
}

console.info('sending', this.field.value.length, 'characters');
this.port.postMessage(
JSON.stringify({
Expand Down Expand Up @@ -340,14 +345,7 @@ function stopGT() {
document.body.classList.remove('GT--waiting');
}

function init() {
const script = document.createElement('script');
script.textContent = '(' + advancedEditors.toString() + ')()';
document.head.append(script);
}

window.startGT = startGT;
window.stopGT = stopGT;

init();
startGT();
1 change: 1 addition & 0 deletions source/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"humane-ghosttext.css",
"humane-ghosttext.js",
"icons/*",
"advanced-editors-messenger.js",
"options-storage.js",
"options.html",
"options.js"
Expand Down

0 comments on commit 0868e41

Please sign in to comment.