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 ...@@ -9,7 +9,49 @@ var getUserDetails = null
var userDetails = {} 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 var baseURL = origin
function objectToQueryString(obj) { function objectToQueryString(obj) {
const keyValuePairs = []; const keyValuePairs = [];
...@@ -26,7 +68,7 @@ var getUserDetails = null ...@@ -26,7 +68,7 @@ var getUserDetails = null
return keyValuePairs.join('&'); return keyValuePairs.join('&');
} }
return { return {
post(url, body={}, headers={}) { post(url, body = {}, headers = {}) {
if (url.slice(0, 4) != 'http') url = (baseURL + `/api${url[0] == '/' ? '' : '/'}${url}`) if (url.slice(0, 4) != 'http') url = (baseURL + `/api${url[0] == '/' ? '' : '/'}${url}`)
return new Promise((res, rej) => { return new Promise((res, rej) => {
let bodyObj = { ...body } let bodyObj = { ...body }
...@@ -51,7 +93,7 @@ var getUserDetails = null ...@@ -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}` if (url.slice(0, 4) != 'http') url = baseURL + `/api${url[0] == '/' ? '' : '/'}${url}`
return new Promise((res, rej) => { return new Promise((res, rej) => {
let bodyObj = { ...body } let bodyObj = { ...body }
...@@ -76,7 +118,7 @@ var getUserDetails = null ...@@ -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}` if (url.slice(0, 4) != 'http') url = baseURL + `/api${url[0] == '/' ? '' : '/'}${url}`
let params = objectToQueryString(obj.params) let params = objectToQueryString(obj.params)
if (params) url += `?${params}` if (params) url += `?${params}`
...@@ -101,7 +143,7 @@ var getUserDetails = null ...@@ -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}` if (url.slice(0, 4) != 'http') url = baseURL + `/api${url[0] == '/' ? '' : '/'}${url}`
let params = objectToQueryString(obj.params) let params = objectToQueryString(obj.params)
if (params) url += `?${params}` if (params) url += `?${params}`
...@@ -152,6 +194,10 @@ var getUserDetails = null ...@@ -152,6 +194,10 @@ var getUserDetails = null
}) })
sendMessageToActiveTab("loggedIn") sendMessageToActiveTab("loggedIn")
setData("userDetails", userDetails)
setData("recording", false)
setData("tutorShotId", tutorShotId)
setData("imageNumber", imageNumber)
return return
} }
...@@ -178,6 +224,9 @@ var getUserDetails = null ...@@ -178,6 +224,9 @@ var getUserDetails = null
status: tutorShotId ? (recording ? "recording" : "paused") : "stopped" status: tutorShotId ? (recording ? "recording" : "paused") : "stopped"
}); });
sendMessageToActiveTab("recordingStarted") sendMessageToActiveTab("recordingStarted")
setData("recording", true)
setData("tutorShotId", tutorShotId)
setData("imageNumber", imageNumber)
return return
} }
if (loggedIn && request.action === 'pauseRecording') { if (loggedIn && request.action === 'pauseRecording') {
...@@ -189,6 +238,7 @@ var getUserDetails = null ...@@ -189,6 +238,7 @@ var getUserDetails = null
status: tutorShotId ? (recording ? "recording" : "paused") : "stopped" status: tutorShotId ? (recording ? "recording" : "paused") : "stopped"
}); });
sendMessageToActiveTab("recordingPaused") sendMessageToActiveTab("recordingPaused")
setData("recording", false)
return return
} }
if (loggedIn && request.action === 'resumeRecording') { if (loggedIn && request.action === 'resumeRecording') {
...@@ -200,6 +250,7 @@ var getUserDetails = null ...@@ -200,6 +250,7 @@ var getUserDetails = null
status: tutorShotId ? (recording ? "recording" : "paused") : "stopped" status: tutorShotId ? (recording ? "recording" : "paused") : "stopped"
}); });
sendMessageToActiveTab("recordingResumed") sendMessageToActiveTab("recordingResumed")
setData("recording", true)
return return
} }
if (loggedIn && request.action === 'stopRecording') { if (loggedIn && request.action === 'stopRecording') {
...@@ -213,6 +264,8 @@ var getUserDetails = null ...@@ -213,6 +264,8 @@ var getUserDetails = null
status: tutorShotId ? (recording ? "recording" : "paused") : "stopped" status: tutorShotId ? (recording ? "recording" : "paused") : "stopped"
}); });
sendMessageToActiveTab("recordingStopped") sendMessageToActiveTab("recordingStopped")
setData("recording", false)
setData("tutorShotId", tutorShotId)
return return
} }
if (loggedIn && request.action === 'restartRecording') { if (loggedIn && request.action === 'restartRecording') {
...@@ -225,6 +278,7 @@ var getUserDetails = null ...@@ -225,6 +278,7 @@ var getUserDetails = null
}); });
await api.delete('/tutor-shot/segments', { params: { tutorShotOid: tutorShotId } }) await api.delete('/tutor-shot/segments', { params: { tutorShotOid: tutorShotId } })
sendMessageToActiveTab("recordingRestarted") sendMessageToActiveTab("recordingRestarted")
setData("imageNumber", 0)
return return
} }
......
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
"description": "A basic Chrome extension", "description": "A basic Chrome extension",
"permissions": [ "permissions": [
"activeTab", "activeTab",
"scripting" "scripting",
"storage"
], ],
"host_permissions": [ "host_permissions": [
"<all_urls>" "<all_urls>"
......
...@@ -78,6 +78,7 @@ ...@@ -78,6 +78,7 @@
function domLoadHandler() { function domLoadHandler() {
chrome.runtime.sendMessage({ action: "checkStatus" }, function (response) { chrome.runtime.sendMessage({ action: "checkStatus" }, function (response) {
console.log('response',response)
userDetails = response.userDetails; userDetails = response.userDetails;
loggedIn = response.loggedIn, loggedIn = response.loggedIn,
recording = response.recording 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