diff --git a/src/js/background/bg_function.js b/src/js/background/bg_function.js index a3b0109..31117e3 100644 --- a/src/js/background/bg_function.js +++ b/src/js/background/bg_function.js @@ -17,6 +17,12 @@ * . */ + +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) { @@ -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}; diff --git a/src/js/background/main.js b/src/js/background/main.js index 8db7a33..715fbce 100644 --- a/src/js/background/main.js +++ b/src/js/background/main.js @@ -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(); @@ -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(); /** diff --git a/src/js/db/customActions.js b/src/js/db/customActions.js index e05140f..ce3109c 100644 --- a/src/js/db/customActions.js +++ b/src/js/db/customActions.js @@ -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"; @@ -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", diff --git a/src/js/db/prefs.js b/src/js/db/prefs.js index b91a2fb..903761d 100644 --- a/src/js/db/prefs.js +++ b/src/js/db/prefs.js @@ -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"; @@ -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}); diff --git a/tests/tests/play.js b/tests/tests/play.js index 56056c1..52e59fe 100644 --- a/tests/tests/play.js +++ b/tests/tests/play.js @@ -37,6 +37,7 @@ const bgGlobalVarName = "cba-test"; const pageSetup = { body: `
Change me
+
Change me2