Skip to content

Commit

Permalink
fix: utils state import into content script (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrani authored Sep 6, 2023
1 parent bd998b3 commit 5ad3fec
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
38 changes: 36 additions & 2 deletions src/content-script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { getState, setState, removeState } from './utils/state.js'

const loadScript = filePath => {
const script = document.createElement('script')
script.src = chrome.runtime.getURL(filePath)
Expand All @@ -9,6 +7,42 @@ const loadScript = filePath => {

loadScript('actions/init.js')

const stateResolver = ({ resolve, reject, result, key, values }) => {
if (chrome.runtime.lastError) {
console.error({ error: chrome.runtime.lastError })
return reject({ error: chrome.runtime.lastError })
}

if (key) return resolve(result?.[key])
if (values) return resolve(values)

return resolve(result)
}

const getState = key => {
return new Promise((resolve, reject) => {
chrome.storage.local.get(key, result => {
return stateResolver({ key, resolve, reject, result })
})
})
}

const removeState = key => {
return new Promise((resolve, reject) => {
chrome.storage.local.remove(key, result => {
return stateResolver({ key, resolve, reject, result })
})
})
}

const setState = ({ key, values }) => {
return new Promise((resolve, reject) => {
chrome.storage.local.set({ [key]: values }, result => {
return stateResolver({ resolve, reject, result, values })
})
})
}

const sendEventToPage = ({ eventType, detail }) => {
window.postMessage({
type: 'FROM_CONTENT_SCRIPT',
Expand Down
35 changes: 0 additions & 35 deletions src/utils/state.js

This file was deleted.

0 comments on commit 5ad3fec

Please sign in to comment.