Skip to content

Commit

Permalink
issue #136 - move actionToPlay to the bg-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Manvel committed Mar 29, 2024
1 parent 730ffc8 commit 835ccf8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/js/background/bg_function.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
* <http://www.gnu.org/licenses/>.
*/


function actionToPlay(actionInd) {
cba.instructArray = cba.defInstructArray.slice(actionInd);
}

async function removeCookie(pattern) {
const cookies = await browser.cookies.getAll({});
for (const cookie of cookies) {
Expand Down Expand Up @@ -84,7 +89,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={"actionId": "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

0 comments on commit 835ccf8

Please sign in to comment.