Skip to content

Commit

Permalink
issue #118 - remove bg-inject from UI (#139)
Browse files Browse the repository at this point in the history
* issue #118 - remove bg-inject from UI

* issue #118 - skip bg-inject actions and adjsut tests
  • Loading branch information
Manvel authored Mar 29, 2024
1 parent 119f93a commit 46a9be9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 132 deletions.
14 changes: 1 addition & 13 deletions src/js/background/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,7 @@ async function actionExecution(instruction)
break;
}
case "bg-inject": {
// eslint-disable-next-line no-unused-vars
let sendInstruction = () => "";
// eslint-disable-next-line no-unused-vars
const actionToPlay = (actionInd) => cba.instructArray = cba.defInstructArray.slice(actionInd);
let sendBgInstruction = true;
const clipboard = cba.clipboard;
await eval(`(async () => {${input1}})()`);
cba.clipboard = clipboard;
if(!sendBgInstruction) {
return new Promise((resolve) => {
sendInstruction = resolve;
});
}
// bg-inject is not supported in MV3.
break;
}
case "inject": {
Expand Down
13 changes: 8 additions & 5 deletions src/js/ui/ActionInputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,14 @@ class ActionInputs {
}

setTooltipInfo() {
const {link, description, name} = actionTypes.filter(({name}) => name === this._type)[0];
const heading = name;
const text = description;
const linkText = "Learn more";
this.tooltip.setData({heading, text, link, linkText});
const action = actionTypes.filter(({name}) => name === this._type)[0];
if (action) {
const {link, description, name} = action;
const heading = name;
const text = description;
const linkText = "Learn more";
this.tooltip.setData({heading, text, link, linkText});
}
}

reset() {
Expand Down
5 changes: 0 additions & 5 deletions src/js/ui/actionsTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ const actionTypes = [
description: "Injects javascript code into the content script.",
link: `${website}/inject-cs`
},
{
name: "bg-inject",
description: "Inject script into backroung page of the extension.",
link: `${website}/bg-inject`
},
{
name: "bg-function",
description: "Predefined scripts that are executed in the context of the background page.",
Expand Down
1 change: 0 additions & 1 deletion tests/tests/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ it("Changing action selectbox disables fields accordingly", async() =>
const disableState = {
"inject": [false, true],
"cs-inject": [false, true],
"bg-inject": [false, true],
"bg-function": [false, true],
"change": [false, false],
"check": [false, true],
Expand Down
121 changes: 16 additions & 105 deletions tests/tests/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ it("Inject function runs specified script in the web page", async() =>
equal(await getTextContent("#changeContent"), newText);
});

it("Executing project with bg-inject skips the bg-inject execution", async() =>
{
const value = "BG injected text";
const data = `window["${bgGlobalVarName}"] = "${value}";`;
const evType = "bg-inject";
const action = createAction(data, evType, "");
const newText = "Injected text";
const injectAction = createAction(setTextContentScript("#changeContent", newText), "inject", "");
await playTestProject([action, injectAction]);
notOk(await getBackgroundGlobalVar(bgGlobalVarName));
equal(await getTextContent("#changeContent"), newText);
});

itIfMV2("cs-inject function runs specified script in content script", async() =>
{
const newText = "CS injected text";
Expand Down Expand Up @@ -130,40 +143,6 @@ itIfMV2("Jquery is accessible through cs-inject", async() =>
equal(await getTextContent(query), newText);
});

itIfMV2("bg-inject function runs specified script in background page", async() =>
{
const value = "BG injected text";
const data = `window["${bgGlobalVarName}"] = "${value}";`;
const evType = "bg-inject";
const action = createAction(data, evType, "");
await playTestProject([action]);
equal(await getBackgroundGlobalVar(bgGlobalVarName), value);
});

itIfMV2("bg-inject action executes script with async(await) code before moving to the next action", async() =>
{
const evType = "bg-inject";

const valuePromise = "Promise action has been played";
const dataPromise = `
window["${bgGlobalVarName}"] = [];
await new Promise(r => setTimeout(()=>
{
window["${bgGlobalVarName}"].push("${valuePromise}");
r();
}, 50));
`;
const actionPromise = createAction(dataPromise, evType, "");

const valueSync = "Sync action has been played after async one";
const dataSync = `window["${bgGlobalVarName}"].push("${valueSync}");`;
const actionSync = createAction(dataSync, evType, "");
await playTestProject([actionPromise, actionSync]);

const backgroundGlobalVar = await getBackgroundGlobalVar(bgGlobalVarName);
deepEqual(await backgroundGlobalVar, [valuePromise, valueSync]);
});

it("bg-function should execute predefined function and play next action when/if defined in function", async() =>
{
await addCookie("https://www.example.com/", "cba", "1");
Expand Down Expand Up @@ -397,61 +376,28 @@ 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 and bg-inject", async() =>
itIfMV2("Clipboard set in inject should be accessible in cs-inject", async() =>
{
const clipboardValue = "cba-test-value";
const clipboardName = "cba-test";
const bgGlobalVarName = "cba-bg-test";
const action1 = createAction(`clipboard["${clipboardName}"] = "${clipboardValue}";`, "inject", "");
const action2 = createAction(setContentFromClipboardScript("#changeContent", clipboardName) , "cs-inject", "");
const action3 = createAction(`window["${bgGlobalVarName}"] = clipboard["${clipboardName}"];`, "bg-inject", "");
await playTestProject([action1, action2, action3]);
await playTestProject([action1, action2]);
await wait();
equal(await getTextContent("#changeContent"), clipboardValue);
equal(await getBackgroundGlobalVar(bgGlobalVarName), clipboardValue);
});

itIfMV2("Clipboard set in cs-inject should be accessible in inject and bg-inject", async() =>
itIfMV2("Clipboard set in cs-inject should be accessible in inject", async() =>
{
const clipboardValue = "cba-test-value";
const clipboardName = "cba-test";
const bgGlobalVarName = "cba-bg-test";
const action1 = createAction(`clipboard["${clipboardName}"] = "${clipboardValue}";`, "cs-inject", "");
const action2 = createAction(setContentFromClipboardScript("#changeContent", clipboardName) , "inject", "");
const action3 = createAction(`window["${bgGlobalVarName}"] = clipboard["${clipboardName}"];`, "bg-inject", "");
await playTestProject([action1, action2, action3]);
await wait();
equal(await getTextContent("#changeContent"), clipboardValue);
equal(await getBackgroundGlobalVar(bgGlobalVarName), clipboardValue);
});

it("Clipboard set in bg-inject should be accessible in inject", async() =>
{
const clipboardValue = "cba-test-value";
const clipboardName = "cba-test";
const action1 = createAction(`clipboard["${clipboardName}"] = "${clipboardValue}";`, "bg-inject", "");
const action2 = createAction(setContentFromClipboardScript("#changeContent", clipboardName) , "inject", "");
await playTestProject([action1, action2]);
await wait();
equal(await getTextContent("#changeContent"), clipboardValue);
});

it("clipboard[...] set as bg-function attribute should be passed along the function call", async() =>
{
await addCookie("https://www.example1.com/", "cba", "1");
await addCookie("https://www.example2.com/", "cba", "1");
ok(await getCookie("https://www.example1.com/", "cba"));
ok(await getCookie("https://www.example2.com/", "cba"));
const clipboardKey = "clip-key";
const clipboardValue = "example1";
const action1 = createAction(`clipboard["${clipboardKey}"] = "${clipboardValue}";`, "bg-inject", "");
const action2 = createAction(`<$function=removeCookie> <$attr=clipboard["${clipboardKey}"]>`, "bg-function", "");
await playTestProject([action1, action2]);
await wait();
notOk(await getCookie("https://www.example1.com/", "cba"));
ok(await getCookie("https://www.example2.com/", "cba"));
});

it("<$unique=> placeholder should generate random number with the specified characters length", async() =>
{
const pasteQuery = "#cba-paste";
Expand Down Expand Up @@ -482,41 +428,6 @@ it("Repeat option should keep repeating actions in the project", async() =>
equal(await getValue("#cba-num"), "5");
});

itIfMV2("sendBgInstruction variable and sendInstruction() method can be used in bg-inject to stop and continue next action invocation", async() =>
{
const firstActionText = "first-action-text";
const secondActionText = "second-action-text";
const bgGlobalVarName = "cba-control-instructions";
const code = `
sendBgInstruction = false;
setTimeout(() => {
window["${bgGlobalVarName}"] = "${firstActionText}";
sendInstruction();
}, 100);`;
const action1 = createAction(code, "bg-inject", "");
const action2 = createAction(`window["${bgGlobalVarName}"] = "${secondActionText}";`, "bg-inject");
await playTestProject([action1, action2]);
await wait(200);
equal(await getBackgroundGlobalVar(bgGlobalVarName), secondActionText);
});

itIfMV2("actionToPlay can be used in bg-inject to Jump to another action", async() =>
{
const query = "#changeContent";
const firstInjectedText = "First Injected Text";
const secondInjectedText = "Second Injected Text";
const jumpToAction = 3;
const lastActionText = "Last action has been played";
const action1 = createAction(setTextContentScript(query, firstInjectedText), "inject");
const action2 = createAction(`actionToPlay(${jumpToAction});`, "bg-inject");
const action3 = createAction(setTextContentScript(query, secondInjectedText), "inject");
const action4 = createAction(`window["${bgGlobalVarName}"] = "${lastActionText}";`, "bg-inject", "");
await playTestProject([action1, action2, action3, action4]);
await wait();
equal(await getTextContent(query), firstInjectedText);
equal(await getBackgroundGlobalVar(bgGlobalVarName), lastActionText);
});

function gotoRedirectPageScript()
{
return `window.location.pathname = "/redirect";`
Expand Down
5 changes: 2 additions & 3 deletions tests/tests/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ it("'Save' button updates selected action with the data from action input fields
await addFourEmptyActions()
await cbaTableSelectRow(cbaTableQuery, "cba-table-id-2");

const type = "bg-inject";
const type = "inject";
const input1 = "testAction1";
const input2 = "testValue1";
await setValue(inputEventQuery, type);
Expand All @@ -329,7 +329,7 @@ it("Selecting action populates input deselecting clears", async() =>
await addFourEmptyActions();
await cbaTableSelectRow(cbaTableQuery, "cba-table-id-2");

const type = "bg-inject";
const type = "inject";
const input1 = "testAction1";
const input2 = "testValue1";

Expand Down Expand Up @@ -408,7 +408,6 @@ it("Changing action selectbox disables fields accordingly", async() =>
const disableState = {
"inject": [false, true],
"cs-inject": [false, true],
"bg-inject": [false, true],
"bg-function": [false, true],
"change": [false, false],
"check": [false, true],
Expand Down

0 comments on commit 46a9be9

Please sign in to comment.