Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #136 - move actionToPlay to the bg-functions #140

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/js/background/bg_function.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
* <http://www.gnu.org/licenses/>.
*/


function actionToPlay(actionIndex) {
const index = parseInt(actionIndex);
cba.instructArray = cba.defInstructArray.slice(index);
}

async function removeCookie(pattern) {
const cookies = await browser.cookies.getAll({});
for (const cookie of cookies) {
Expand Down Expand Up @@ -84,7 +90,7 @@ async function request(url, type, data = "", contentType, resolveJson = true)
return response;
}

module.exports = {removeCookie, saveToClipboard,
module.exports = {actionToPlay, removeCookie, saveToClipboard,
panelCreation, windowCreation,
removeCurrentWindow, reloadCurrentTab,
requestService};
17 changes: 17 additions & 0 deletions src/js/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ if (!process.env.MV3) {
const {CBA} = require("./CBA");
const {playProject} = require("./actions");
const projectsDb = require("../db/projects");
const prefs = require("../db/prefs");
const customActionsDb = require("../db/customActions");
const {addRpcListener, sendRpcMessageResponse} = require("../rpc/host");
const {setBadgeText} = require("./utils");
const latestMigrationVersion = 1;

/** @global */
globalThis.cba = new CBA();
Expand Down Expand Up @@ -96,9 +98,24 @@ async function isFirstLoad() {
const dbItem = {};
dbItem[customActionsDb.name] = customActionsDb.predefined;
await browser.storage.local.set(dbItem);
prefs.set("migrationVersion", latestMigrationVersion);
} else {
const migrationVersion = await prefs.get("migrationVersion");
if (!migrationVersion) {
// This is first migration, followups needs to be compared against number.
addNewFunctions();
prefs.set("migrationVersion", latestMigrationVersion);
}
}
}

async function addNewFunctions() {
const customActions = await customActionsDb.load();
const firstMigrationItems = customActionsDb.predefined.filter((item) => item.migrationVersion && item.migrationVersion == 1);
customActions.push(...firstMigrationItems);
customActionsDb.saveState(customActions);
}

isFirstLoad();

/**
Expand Down
12 changes: 12 additions & 0 deletions src/js/db/customActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @property {Action} data - Action.
* @property {CustomActionInfo} info - More information.
* @property {string} text - Name.
* @property {number} [migrationVersion] - Migration version.
*/

const name = "customActions";
Expand All @@ -60,6 +61,17 @@ function saveState(items) {
* @type {CustomAction[]}
*/
const predefined = [
{
data: {
type: "bg-function",
inputs: ['<$function=actionToPlay>\n<$attr=number>', "Jump to another action while executing the project."]
},
info: {
description: "Jump to another action while executing the project."
},
text: "Action to play",
migrationVersion: 1
},
{
data: {
type: "timer",
Expand Down
3 changes: 2 additions & 1 deletion src/js/db/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @property {boolean} hidePowerfulActionWarning - Hide tooltip for powerful actions.
* @property {string|null} lastSelectedProjectId - The ID of the last selected project.
* @property {string|null} lastSelectedActionId - The ID of the last selected action.
* @property {number} migrationVersion - number of the migration.
*/

const name = "prefs";
Expand Down Expand Up @@ -74,7 +75,7 @@ async function get(pref)
async function set(pref, value)
{
const prefs = await load();
if (prefs && pref in prefs)
if (prefs)
{
prefs[pref] = value;
return browser.storage.local.set({prefs});
Expand Down
19 changes: 19 additions & 0 deletions tests/tests/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const bgGlobalVarName = "cba-test";
const pageSetup = {
body: `
<div id="changeContent">Change me</div>
<div id="changeContent2">Change me2</div>
<div id="cba-change">
<input id="cba-textbox" type="text" />
<select id="cba-selectbox">
Expand Down Expand Up @@ -208,6 +209,24 @@ it("bg-function reloadCurrentTab(without attributes test) should reload current
equal(await getTextContent(query), newText);
});

it("bg-function actionToPlay should jump to another action", async() =>
{
const query = "#changeContent";
const query2 = "#changeContent2";
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(`<$function=actionToPlay>\n<$attr=${jumpToAction}>`, "bg-function");
const action3 = createAction(setTextContentScript(query, secondInjectedText), "inject");
const action4 = createAction(setTextContentScript(query2, lastActionText), "inject");
await playTestProject([action1, action2, action3, action4]);
await wait();
equal(await getTextContent(query), firstInjectedText);
equal(await getTextContent(query2), lastActionText);
});

it("Change action updates value of a textbox, focuses and fires a change event", async() =>
{
const newText = "Injected value";
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ it("Selecting action populates input deselecting clears", async() =>
it("dragndropping from the functions table or self-organizing actions table should update actions accordingly", async() =>
{
await addFourEmptyActions();
const handle = await getCbaListRowHandle(cbaFunctionsQuery, "cba-list-id-1");
const handle = await getCbaListRowHandle(cbaFunctionsQuery, "cba-list-id-2");

const actionTableItemIsTimer = async(index) =>
{
Expand Down
Loading