From 2ed6bbfa3fc6a89db15edd65d6c1b059e289aeb9 Mon Sep 17 00:00:00 2001 From: Manvel Saroyan Date: Fri, 29 Mar 2024 19:01:57 -0300 Subject: [PATCH] issue #141 - execute inject action when cs-inject is used (#142) --- src/js/background/actions.js | 18 ++++++++++++++++-- tests/tests/play.js | 16 +++++++++------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/js/background/actions.js b/src/js/background/actions.js index f62ecc1..c1f382d 100644 --- a/src/js/background/actions.js +++ b/src/js/background/actions.js @@ -81,6 +81,7 @@ async function actionExecution(instruction) // bg-inject is not supported in MV3. break; } + case "cs-inject": case "inject": { if (!process.env.MV3) { await messageContentScript(instruction, cba.clipboard); @@ -111,14 +112,27 @@ async function actionExecution(instruction) document.getElementById('${clipboardId}').style.display = 'none';`; document.documentElement.appendChild(script); // run the script document.documentElement.removeChild(script); // clean up + }, + args: [cba.clipboard, input1], + world: "MAIN" + }); + + // Retrieve clipboard value + const [clipboard] = await browser.scripting.executeScript({ + target: {tabId: playingTabId}, + func: () => { + const clipboardId = "grabClipboardHere"; const injectedClipboard = document.querySelector(`#${clipboardId}`); if(injectedClipboard) { - clipboard = JSON.parse(injectedClipboard.textContent); + return JSON.parse(injectedClipboard.textContent); } + return {}; }, - args: [cba.clipboard, input1], world: "MAIN" }); + if (clipboard && clipboard.result) { + cba.clipboard = clipboard.result; + } break; } case "pause": { diff --git a/tests/tests/play.js b/tests/tests/play.js index ab25b06..7300ed7 100644 --- a/tests/tests/play.js +++ b/tests/tests/play.js @@ -60,8 +60,6 @@ const pageSetup = { path: server } -const itIfMV2 = !process.env.MV3 ? it : it.skip; - beforeEach(async () => { const pageUrl = await getPageUrl(); @@ -105,7 +103,7 @@ it("Executing project with bg-inject skips the bg-inject execution", async() => equal(await getTextContent("#changeContent"), newText); }); -itIfMV2("cs-inject function runs specified script in content script", async() => +it("cs-inject function runs specified script in content script", async() => { const newText = "CS injected text"; const evType = "cs-inject"; @@ -114,7 +112,10 @@ itIfMV2("cs-inject function runs specified script in content script", async() => equal(await getTextContent("#changeContent"), newText); }); -itIfMV2("cs-inject action executes script with async(await) code before moving to the next action", async() => +/* +Can't support unless https://github.com/w3c/webextensions/pull/540 is implemented +by the browsers. +it("cs-inject action executes script with async(await) code before moving to the next action", async() => { const evType = "cs-inject"; const valuePromise = "Promise action has been played"; @@ -134,7 +135,7 @@ itIfMV2("cs-inject action executes script with async(await) code before moving t equal(await getTextContent("#changeContent"), valuePromise+valueSync); }); -itIfMV2("Jquery is accessible through cs-inject", async() => +it("Jquery is accessible through cs-inject", async() => { const newText = "Jquery in CS injected text"; const query = "#changeContent"; @@ -142,6 +143,7 @@ itIfMV2("Jquery is accessible through cs-inject", async() => await playTestProject([action]); equal(await getTextContent(query), newText); }); +*/ it("bg-function should execute predefined function and play next action when/if defined in function", async() => { @@ -376,7 +378,7 @@ it("Pause action pauses the workflow until the project is played again and set ' equal(await getBadgeText(), ""); }); -itIfMV2("Clipboard set in inject should be accessible in cs-inject", async() => +it("Clipboard set in inject should be accessible in cs-inject", async() => { const clipboardValue = "cba-test-value"; const clipboardName = "cba-test"; @@ -387,7 +389,7 @@ itIfMV2("Clipboard set in inject should be accessible in cs-inject", async() => equal(await getTextContent("#changeContent"), clipboardValue); }); -itIfMV2("Clipboard set in cs-inject should be accessible in inject", async() => +it("Clipboard set in cs-inject should be accessible in inject", async() => { const clipboardValue = "cba-test-value"; const clipboardName = "cba-test";