Skip to content

Commit

Permalink
issue #141 - execute inject action when cs-inject is used (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manvel committed Mar 29, 2024
1 parent 46a9be9 commit 2ed6bbf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
18 changes: 16 additions & 2 deletions src/js/background/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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": {
Expand Down
16 changes: 9 additions & 7 deletions tests/tests/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ const pageSetup = {
path: server
}

const itIfMV2 = !process.env.MV3 ? it : it.skip;

beforeEach(async () =>
{
const pageUrl = await getPageUrl();
Expand Down Expand Up @@ -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";
Expand All @@ -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";
Expand All @@ -134,14 +135,15 @@ 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";
const action = createAction(`$("${query}").text("${newText}")`, "cs-inject", "");
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() =>
{
Expand Down Expand Up @@ -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";
Expand All @@ -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";
Expand Down

0 comments on commit 2ed6bbf

Please sign in to comment.