Skip to content

Commit 03267bc

Browse files
committed
update element selector
1 parent 1dce7c7 commit 03267bc

File tree

12 files changed

+891
-731
lines changed

12 files changed

+891
-731
lines changed

buildScripts/build-chrome.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const manifest = {
7272
content_scripts: [
7373
{
7474
matches: ["http://*/*", "https://*/*"],
75-
js: ["utils/elementSelector.js"],
75+
js: ["elementSelector/main.js"],
7676
css: ["assets/styles/elementSelector.css"],
7777
run_at: "document_start",
7878
},
@@ -106,7 +106,7 @@ await build({
106106
entryPoints: [
107107
"src/background/background.js",
108108
"src/utils/content_bridge.js",
109-
"src/utils/elementSelector.js",
109+
"src/elementSelector/main.js",
110110
"src/GM/gm_core.js",
111111
"src/utils/greasyfork_interceptor.js",
112112
"src/utils/inject.js",

buildScripts/build-firefox.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ await build({
8181
entryPoints: [
8282
"src/background/background.js",
8383
"src/utils/content_bridge.js",
84-
"src/utils/elementSelector.js",
84+
"src/elementSelector/main.js",
8585
"src/GM/gm_core.js",
8686
"src/utils/greasyfork_interceptor.js",
8787
"src/utils/inject.js",

src/GM/gm_bridge.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ export class GMBridge {
7474
// Use direct chrome.runtime API if available (ISOLATED world)
7575
if (
7676
this.worldType === "ISOLATED" &&
77-
typeof chrome?.runtime?.sendMessage === "function"
77+
typeof chrome !== "undefined" &&
78+
chrome.runtime &&
79+
typeof chrome.runtime.sendMessage === "function"
7880
) {
7981
return this.callIsolated(action, enrichedPayload);
8082
}
@@ -103,14 +105,30 @@ export class GMBridge {
103105

104106
callIsolated(action, payload = {}) {
105107
return new Promise((resolve, reject) => {
108+
// Defensive: ensure chrome.runtime.sendMessage still available
109+
if (
110+
!(
111+
typeof chrome !== "undefined" &&
112+
chrome.runtime &&
113+
typeof chrome.runtime.sendMessage === "function"
114+
)
115+
) {
116+
reject(new Error("chrome.runtime.sendMessage is not available"));
117+
return;
118+
}
119+
106120
try {
107121
chrome.runtime.sendMessage(
108122
{
109123
type: "GM_API_REQUEST",
110124
payload: { action, ...payload },
111125
},
112126
(response) => {
113-
if (chrome.runtime.lastError) {
127+
if (
128+
typeof chrome !== "undefined" &&
129+
chrome.runtime &&
130+
chrome.runtime.lastError
131+
) {
114132
reject(new Error(chrome.runtime.lastError.message));
115133
return;
116134
}

src/GM/gm_core.js

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -229,35 +229,25 @@ if (window.GMBridge === undefined) {
229229
// Load external dependencies
230230
await loader.loadScripts(requireUrls);
231231

232-
// Wrap user code in IIFE with unsafeWindow
233-
const wrappedCode = `(function() {
234-
'use strict';
235-
const unsafeWindow = this;
236-
${userCode}
237-
}).call(window);
238-
`;
239-
240-
// Create and inject script element
241-
const scriptElement = document.createElement("script");
242-
scriptElement.setAttribute("data-script-id", scriptId);
243-
244-
// Apply Trusted Types if available
245232
const policy = getTrustedTypesPolicy();
246-
let trustedCode = wrappedCode;
247-
248233
if (policy) {
249-
try {
250-
trustedCode = policy.createScript(wrappedCode);
251-
} catch (error) {
252-
console.error("[GMBridge] Failed to create trusted script:", error);
253-
}
234+
// With Trusted Types, we must use a script tag, which runs in the MAIN world.
235+
// This is a known limitation for ISOLATED world scripts.
236+
const wrappedCode = `(function() { 'use strict'; const unsafeWindow = this; ${userCode} }).call(window);`;
237+
const scriptElement = document.createElement("script");
238+
scriptElement.setAttribute("data-script-id", scriptId);
239+
scriptElement.textContent = policy.createScript(wrappedCode);
240+
(
241+
document.head ||
242+
document.documentElement ||
243+
document.body
244+
).appendChild(scriptElement);
245+
scriptElement.remove();
246+
} else {
247+
// No Trusted Types, we can execute in the current world's scope.
248+
const run = new Function("unsafeWindow", userCode);
249+
run.call(window, window);
254250
}
255-
256-
scriptElement.textContent = trustedCode;
257-
(document.head || document.documentElement || document.body).appendChild(
258-
scriptElement
259-
);
260-
scriptElement.remove();
261251
} catch (error) {
262252
console.error(
263253
`[GMBridge] Error executing user script ${scriptId}:`,

src/ai_dom_editor/editor/ai_dom_editor.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ class AIDOMEditor {
4343
async init() {
4444
applyTheme();
4545
await this.initializeAI();
46+
47+
chrome.runtime.onMessage.addListener((message) => {
48+
if (message.action === "aiElementSelected") {
49+
this.elements.userInput.value += ` ${message.selector} `;
50+
this.elements.userInput.focus();
51+
this.uiManager.deactivateElementSelector();
52+
}
53+
});
4654
}
4755

4856
setCurrentScript(scriptName) {

src/ai_dom_editor/editor/helpers/ui_manager.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ export class UIManager {
288288
this.editor.elements.elementSelectorBtn.classList.add("active");
289289

290290
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
291-
chrome.tabs.sendMessage(tabs[0].id, { action: "startSelection" });
291+
if (tabs.length > 0) {
292+
chrome.tabs.sendMessage(tabs[0].id, { action: "startAiSelection" });
293+
}
292294
});
293295
}
294296

@@ -297,7 +299,9 @@ export class UIManager {
297299
this.editor.elements.elementSelectorBtn.classList.remove("active");
298300

299301
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
300-
chrome.tabs.sendMessage(tabs[0].id, { action: "stopSelection" });
302+
if (tabs.length > 0) {
303+
chrome.tabs.sendMessage(tabs[0].id, { action: "stopSelection" });
304+
}
301305
});
302306
}
303307

0 commit comments

Comments
 (0)