Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tutor-shot-extension
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ramdayal Munda
tutor-shot-extension
Commits
89279b51
Commit
89279b51
authored
Nov 15, 2023
by
ramdayalmunda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added custom fetch query to send request from background.js
parent
a5ffea72
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
129 additions
and
30 deletions
+129
-30
background.js
background.js
+127
-29
content.js
content.js
+2
-1
No files found.
background.js
View file @
89279b51
...
...
@@ -8,32 +8,128 @@ var getUserDetails = null
var
loggedIn
=
false
;
var
userDetails
=
{}
function
sendReq
(
url
=
origin
,
method
=
'GET'
,
body
=
{},
headers
=
{}){
if
(
url
.
slice
(
0
,
4
)
!=
'http'
)
url
=
origin
+=
`/api
${
url
[
0
]
==
'/'
?
''
:
'/'
}${
url
}
`
return
new
Promise
(
(
res
,
rej
)
=>
{
function
createAPI
(
origin
){
var
baseURL
=
origin
function
objectToQueryString
(
obj
)
{
const
keyValuePairs
=
[];
for
(
const
key
in
obj
)
{
if
(
obj
.
hasOwnProperty
(
key
))
{
const
value
=
obj
[
key
];
if
(
value
!==
undefined
)
{
keyValuePairs
.
push
(
encodeURIComponent
(
key
)
+
'='
+
encodeURIComponent
(
value
));
}
}
}
return
keyValuePairs
.
join
(
'&'
);
}
return
{
post
(
url
,
body
=
{},
headers
=
{})
{
if
(
url
.
slice
(
0
,
4
)
!=
'http'
)
url
=
(
baseURL
+
`/api
${
url
[
0
]
==
'/'
?
''
:
'/'
}${
url
}
`
)
return
new
Promise
((
res
,
rej
)
=>
{
let
bodyObj
=
{
...
body
}
let
headerObj
=
{
'Content-Type'
:
'application/json'
,
...
headers
,
}
fetch
(
url
,
{
method
:
'POST'
,
body
:
JSON
.
stringify
({
...
bodyObj
}),
headers
:
headerObj
,
}).
then
(
async
(
data
)
=>
{
if
(
headerObj
[
'Content-Type'
]
==
'application/json'
)
{
res
(
await
data
.
json
())
}
else
{
res
(
data
)
}
}).
catch
(
err
=>
{
console
.
log
(
'fetch err'
,
err
)
rej
(
err
)
})
})
},
put
(
url
,
body
=
{},
headers
=
{})
{
if
(
url
.
slice
(
0
,
4
)
!=
'http'
)
url
=
baseURL
+
`/api
${
url
[
0
]
==
'/'
?
''
:
'/'
}${
url
}
`
return
new
Promise
((
res
,
rej
)
=>
{
let
bodyObj
=
{
...
body
}
let
headerObj
=
{
'Content-Type'
:
'application/json'
,
...
headers
...
headers
,
}
fetch
(
url
,
{
method
:
method
,
fetch
(
url
,
{
method
:
'PUT'
,
body
:
JSON
.
stringify
({
...
bodyObj
}),
headers
:
headerObj
,
}
).
then
(
async
(
data
)
=>
{
if
(
headerObj
[
'Content-Type'
]
==
'application/json'
){
res
(
await
data
.
json
()
)
}
else
{
}).
then
(
async
(
data
)
=>
{
if
(
headerObj
[
'Content-Type'
]
==
'application/json'
)
{
res
(
await
data
.
json
())
}
else
{
res
(
data
)
}
}).
catch
(
err
=>
{
console
.
log
(
'fetch err'
,
err
)
rej
(
err
)
})
})
},
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
}
`
return
new
Promise
((
res
,
rej
)
=>
{
let
headerObj
=
{
'Content-Type'
:
'application/json'
,
...
obj
.
headers
,
}
fetch
(
url
,
{
method
:
'DELETE'
,
headers
:
headerObj
,
}).
then
(
async
(
data
)
=>
{
if
(
headerObj
[
'Content-Type'
]
==
'application/json'
)
{
res
(
await
data
.
json
())
}
else
{
res
(
data
)
}
}).
catch
(
err
=>
{
console
.
log
(
'fetch err'
,
err
)
rej
(
err
)
})
})
},
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
}
`
return
new
Promise
((
res
,
rej
)
=>
{
let
headerObj
=
{
'Content-Type'
:
'application/json'
,
...
obj
.
headers
,
}
fetch
(
url
,
{
method
:
'DELETE'
,
headers
:
headerObj
,
}).
then
(
async
(
data
)
=>
{
if
(
headerObj
[
'Content-Type'
]
==
'application/json'
)
{
res
(
await
data
.
json
())
}
else
{
res
(
data
)
}
}
).
catch
(
err
=>
{
}).
catch
(
err
=>
{
console
.
log
(
'fetch err'
,
err
)
rej
(
err
)
})
}
)
})
},
objectToQueryString
}
}
function
onMessage
(
request
,
sender
,
sendResponse
)
{
var
api
=
createAPI
(
origin
)
async
function
onMessage
(
request
,
sender
,
sendResponse
)
{
if
(
request
.
action
===
'login'
)
{
console
.
log
(
'B: to logged in'
,
request
.
userDetails
)
...
...
@@ -49,7 +145,7 @@ var getUserDetails = null
userDetails
,
tutorShotOid
:
tutorShotId
,
imageNumber
:
imageNumber
,
status
:
tutorShotId
?(
recording
?
"recording"
:
"paused"
):
"stopped"
,
status
:
tutorShotId
?
(
recording
?
"recording"
:
"paused"
)
:
"stopped"
,
})
sendMessageToActiveTab
(
"loggedIn"
)
...
...
@@ -63,7 +159,7 @@ var getUserDetails = null
recording
,
tutorShotOid
:
tutorShotId
,
imageNumber
:
imageNumber
,
status
:
tutorShotId
?(
recording
?
"recording"
:
"paused"
):
"stopped"
status
:
tutorShotId
?
(
recording
?
"recording"
:
"paused"
)
:
"stopped"
})
return
}
...
...
@@ -76,7 +172,7 @@ var getUserDetails = null
userDetails
,
loggedIn
,
recording
,
status
:
tutorShotId
?(
recording
?
"recording"
:
"paused"
):
"stopped"
status
:
tutorShotId
?
(
recording
?
"recording"
:
"paused"
)
:
"stopped"
});
sendMessageToActiveTab
(
"recordingStarted"
)
return
...
...
@@ -87,7 +183,7 @@ var getUserDetails = null
userDetails
,
loggedIn
,
recording
,
status
:
tutorShotId
?(
recording
?
"recording"
:
"paused"
):
"stopped"
status
:
tutorShotId
?
(
recording
?
"recording"
:
"paused"
)
:
"stopped"
});
sendMessageToActiveTab
(
"recordingPaused"
)
return
...
...
@@ -98,7 +194,7 @@ var getUserDetails = null
userDetails
,
loggedIn
,
recording
,
status
:
tutorShotId
?(
recording
?
"recording"
:
"paused"
):
"stopped"
status
:
tutorShotId
?
(
recording
?
"recording"
:
"paused"
)
:
"stopped"
});
sendMessageToActiveTab
(
"recordingResumed"
)
return
...
...
@@ -110,8 +206,10 @@ var getUserDetails = null
userDetails
,
loggedIn
,
recording
,
status
:
tutorShotId
?(
recording
?
"recording"
:
"paused"
):
"stopped"
status
:
tutorShotId
?
(
recording
?
"recording"
:
"paused"
)
:
"stopped"
});
await
api
.
post
(
'/tutor-shot/finalize'
,
{
_id
:
tutorShotId
},
{
headers
:
{
userid
:
userDetails
.
uid
}
})
sendMessageToActiveTab
(
"recordingStopped"
)
return
}
...
...
@@ -121,7 +219,7 @@ var getUserDetails = null
userDetails
,
loggedIn
,
recording
,
status
:
tutorShotId
?(
recording
?
"recording"
:
"paused"
):
"stopped"
status
:
tutorShotId
?
(
recording
?
"recording"
:
"paused"
)
:
"stopped"
});
sendMessageToActiveTab
(
"recordingRestarted"
)
return
...
...
@@ -136,7 +234,7 @@ var getUserDetails = null
console
.
log
(
'recording not started for SS'
);
return
;
}
if
(
!
tutorShotId
){
if
(
!
tutorShotId
)
{
console
.
log
(
'how to set not created'
);
return
}
...
...
@@ -149,14 +247,14 @@ var getUserDetails = null
tutorShotOid
:
tutorShotId
,
action
:
'SSCaptured'
,
dataUrl
:
dataUrl
,
imageNumber
:
imageNumber
++
,
status
:
tutorShotId
?(
recording
?
"recording"
:
"paused"
):
"stopped"
,
status
:
tutorShotId
?
(
recording
?
"recording"
:
"paused"
)
:
"stopped"
,
...
request
.
captureInfo
?
request
.
captureInfo
:
{}
}
// to send message to the content instance of the
chrome
.
tabs
.
sendMessage
(
activeTab
.
id
,
message
);
console
.
log
(
'to take screenshot'
,
message
)
let
data
=
await
sendReq
(
'/tutor-shot/screen-shot'
,
'POST
'
,
message
)
console
.
log
(
'sent to server'
,
data
)
let
data
=
await
api
.
post
(
'/tutor-shot/screen-shot
'
,
message
)
console
.
log
(
'sent
SS
to server'
,
data
)
})
}
})
...
...
@@ -178,23 +276,23 @@ var getUserDetails = null
return
dataSet
}
function
sendMessageToActiveTab
(
messageObj
){
try
{
function
sendMessageToActiveTab
(
messageObj
)
{
try
{
chrome
.
tabs
.
query
({
active
:
true
,
currentWindow
:
true
},
function
(
tabs
)
{
let
activeTab
=
tabs
[
0
]
if
(
activeTab
?.
url
?.
slice
(
0
,
6
)
!=
'chrome'
)
{
// if any tab is active
if
(
activeTab
?.
url
?.
slice
(
0
,
6
)
!=
'chrome'
)
{
// if any tab is active
let
message
=
{
userDetails
,
tutorShotOid
:
tutorShotId
,
imageNumber
:
imageNumber
,
status
:
tutorShotId
?(
recording
?
"recording"
:
"paused"
):
"stopped"
,
status
:
tutorShotId
?
(
recording
?
"recording"
:
"paused"
)
:
"stopped"
,
message
:
messageObj
}
console
.
log
(
'sending message:'
,
message
)
chrome
.
tabs
.
sendMessage
(
activeTab
.
id
,
message
);
}
})
}
catch
(
err
)
{
}
catch
(
err
)
{
console
.
log
(
err
)
}
}
...
...
content.js
View file @
89279b51
...
...
@@ -377,7 +377,8 @@ async function resumeRecording() {
async
function
stopRecording
()
{
try
{
await
api
.
post
(
'/tutor-shot/finalize'
,
{
_id
:
tutorShotOid
},
{
headers
:
{
userid
:
userDetails
.
uid
}
})
console
.
log
(
'to stop recording'
)
// await api.post('/tutor-shot/finalize', { _id: tutorShotOid }, { headers: { userid: userDetails.uid } })
await
chrome
.
runtime
.
sendMessage
({
action
:
"stopRecording"
})
}
catch
(
err
)
{
console
.
log
(
err
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment