Skip to content

Commit

Permalink
Merge pull request #637 from hababr/fix-js-csp
Browse files Browse the repository at this point in the history
Fix function name and add missing key IDs
  • Loading branch information
crittermike authored Aug 15, 2024
2 parents 31746ae + 75d9445 commit 28770a7
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions app/scripts/service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import captureScreenshot from './actions/captureScreenshot'
import lastUsedTab from './actions/lastUsedTab'
import { executeScript } from './utils'
import { v4 as uuid } from "uuid"

/* global localStorage, chrome */

Expand Down Expand Up @@ -377,6 +378,16 @@ let handleAction = async (action, request = {}) => {
return true
}

async function checkKeys() {
const keys = JSON.parse((await chrome.storage.local.get("keys")).keys) || []
for (const key of keys) {
if (!key.id) {
key.id = uuid()
}
}
await chrome.storage.local.set({ keys: JSON.stringify(keys) });
}

async function registerUserScript() {
const keys = JSON.parse((await chrome.storage.local.get("keys")).keys) || []
const javascriptActions = keys.filter(key => key.action === "javascript")
Expand Down Expand Up @@ -406,7 +417,7 @@ async function registerUserScript() {
world: "MAIN",
js: [
{
code: `const handlers = ${actionHandlersAsObject};\n${registerHandlers.toString()}\nregisterHandlers();`
code: `const handlers = ${actionHandlersAsObject};\n(${registerHandlers.toString()})();`
}
]
}
Expand All @@ -421,8 +432,9 @@ async function registerUserScript() {

chrome.storage.local.onChanged.addListener(registerUserScript)

chrome.runtime.onInstalled.addListener(function (details) {
chrome.runtime.onInstalled.addListener(async function (details) {
if (details.reason === "update") {
await checkKeys()
registerUserScript()
}
})
Expand Down

0 comments on commit 28770a7

Please sign in to comment.