Improve compatibility with CSP pages (#315)

This commit is contained in:
Federico Brigante 2024-06-30 17:27:50 +08:00 committed by GitHub
parent 331c70f7f0
commit 3fb653eea1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 17 deletions

View File

@ -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);
@ -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');

View File

@ -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;
}
},
@ -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) {

View File

@ -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();
@ -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({
@ -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);

View File

@ -35,7 +35,7 @@
"browser_specific_settings": {
"gecko": {
"id": "ghosttext@bfred.it",
"strict_min_version": "120.0"
"strict_min_version": "128.0"
}
},
"options_ui": {
@ -91,6 +91,7 @@
"humane-ghosttext.css",
"humane-ghosttext.js",
"icons/*",
"advanced-editors-messenger.js",
"options-storage.js",
"options.html",
"options.js"