Commit 3b4e49e5 by ramdayalmunda

storing the data locally so even if the BG process is inavtive we can retrieve the data

parent fc770615
......@@ -9,7 +9,49 @@ var getUserDetails = null
var userDetails = {}
function createAPI(origin){
function setData(key, value, callback) {
return new Promise((res, rej) => {
try {
chrome.storage.local.set({ [key]: value }, () => {
if (callback) callback(value)
else res(value)
})
} catch (err) {
if (callback) callback(undefined, err)
else callback(undefined)
}
})
}
function getData(key, callback) {
return new Promise((res, rej) => {
try {
chrome.storage.local.get(key, (data) => {
if (callback) callback(data[key])
else res(data[key])
})
} catch (err) {
if (callback) callback(undefined, err)
else res(undefined)
}
})
}
getData('userDetails', async (data)=>{
if (data){
let tso = await getData('tutorShot')
tutorShotId = tso?tso:null
let imgNum = await getData('imageNumber')
imageNumber = imgNum?imgNum:0
let rec = await getData('recording')
recording = rec?rec:false
loggedIn = true
userDetails = data
}
})
function createAPI(origin) {
var baseURL = origin
function objectToQueryString(obj) {
const keyValuePairs = [];
......@@ -26,7 +68,7 @@ var getUserDetails = null
return keyValuePairs.join('&');
}
return {
post(url, body={}, headers={}) {
post(url, body = {}, headers = {}) {
if (url.slice(0, 4) != 'http') url = (baseURL + `/api${url[0] == '/' ? '' : '/'}${url}`)
return new Promise((res, rej) => {
let bodyObj = { ...body }
......@@ -51,7 +93,7 @@ var getUserDetails = null
})
})
},
put(url, body={}, headers={}) {
put(url, body = {}, headers = {}) {
if (url.slice(0, 4) != 'http') url = baseURL + `/api${url[0] == '/' ? '' : '/'}${url}`
return new Promise((res, rej) => {
let bodyObj = { ...body }
......@@ -76,7 +118,7 @@ var getUserDetails = null
})
})
},
delete(url, obj={ params: {}, headers: {} }){
delete(url, obj = { params: {}, headers: {} }) {
if (url.slice(0, 4) != 'http') url = baseURL + `/api${url[0] == '/' ? '' : '/'}${url}`
let params = objectToQueryString(obj.params)
if (params) url += `?${params}`
......@@ -101,7 +143,7 @@ var getUserDetails = null
})
})
},
get(url, obj={ params: {}, headers: {} }){
get(url, obj = { params: {}, headers: {} }) {
if (url.slice(0, 4) != 'http') url = baseURL + `/api${url[0] == '/' ? '' : '/'}${url}`
let params = objectToQueryString(obj.params)
if (params) url += `?${params}`
......@@ -152,6 +194,10 @@ var getUserDetails = null
})
sendMessageToActiveTab("loggedIn")
setData("userDetails", userDetails)
setData("recording", false)
setData("tutorShotId", tutorShotId)
setData("imageNumber", imageNumber)
return
}
......@@ -178,6 +224,9 @@ var getUserDetails = null
status: tutorShotId ? (recording ? "recording" : "paused") : "stopped"
});
sendMessageToActiveTab("recordingStarted")
setData("recording", true)
setData("tutorShotId", tutorShotId)
setData("imageNumber", imageNumber)
return
}
if (loggedIn && request.action === 'pauseRecording') {
......@@ -189,6 +238,7 @@ var getUserDetails = null
status: tutorShotId ? (recording ? "recording" : "paused") : "stopped"
});
sendMessageToActiveTab("recordingPaused")
setData("recording", false)
return
}
if (loggedIn && request.action === 'resumeRecording') {
......@@ -200,6 +250,7 @@ var getUserDetails = null
status: tutorShotId ? (recording ? "recording" : "paused") : "stopped"
});
sendMessageToActiveTab("recordingResumed")
setData("recording", true)
return
}
if (loggedIn && request.action === 'stopRecording') {
......@@ -213,6 +264,8 @@ var getUserDetails = null
status: tutorShotId ? (recording ? "recording" : "paused") : "stopped"
});
sendMessageToActiveTab("recordingStopped")
setData("recording", false)
setData("tutorShotId", tutorShotId)
return
}
if (loggedIn && request.action === 'restartRecording') {
......@@ -225,6 +278,7 @@ var getUserDetails = null
});
await api.delete('/tutor-shot/segments', { params: { tutorShotOid: tutorShotId } })
sendMessageToActiveTab("recordingRestarted")
setData("imageNumber", 0)
return
}
......
......@@ -5,7 +5,8 @@
"description": "A basic Chrome extension",
"permissions": [
"activeTab",
"scripting"
"scripting",
"storage"
],
"host_permissions": [
"<all_urls>"
......
......@@ -78,6 +78,7 @@
function domLoadHandler() {
chrome.runtime.sendMessage({ action: "checkStatus" }, function (response) {
console.log('response',response)
userDetails = response.userDetails;
loggedIn = response.loggedIn,
recording = response.recording
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment