Skip to content

Commit

Permalink
Improve compatibility with CSP pages (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Jun 30, 2024
1 parent 331c70f commit 3fb653e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 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 ready');
11 changes: 8 additions & 3 deletions source/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ async function handleAction({id}) {
// eslint-disable-next-line object-shorthand -- Chrome hates it
func: () => {
try {
console.log('Starting GT');
// eslint-disable-next-line no-undef -- Different context
startGT();
return 'ready';
return true;
} catch {
console.log('not ready');
return false;
}
},
Expand All @@ -58,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
16 changes: 7 additions & 9 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();
setTimeout(startGT, 100);
3 changes: 2 additions & 1 deletion source/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"browser_specific_settings": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "120.0"
"strict_min_version": "128.0"
}
},
"options_ui": {
Expand Down 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 3fb653e

Please sign in to comment.