@@ -7,56 +7,54 @@ import { isScreenshotSenderTab } from "../shared/native-screenshoting/message";
77import { Post } from "../shared/model/post" ;
88
99export default defineBackground ( ( ) => {
10- console . log ( "Hello background!" , { extensionId : browser . runtime . id } ) ;
11-
12- function handleMessages (
13- message : unknown ,
14- sender : Browser . runtime . MessageSender ,
15- sendResponse : ( response : unknown ) => void ,
16- ) {
17- console . debug ( "Background - Message received:" , message , sender ) ;
18-
19- if ( isScrapActiveTabMessage ( message ) ) {
20- scrapActiveTab ( ) ;
21- return ;
22- } else if ( isScreenshotSenderTab ( message ) ) {
23- screenshotSenderTab ( sender ) . then ( ( data ) => {
24- sendResponse ( data ) ;
25- } ) ;
26- return true ;
27- }
10+ console . log ( "Hello background!" , { extensionId : browser . runtime . id } ) ;
11+
12+ function handleMessages (
13+ message : unknown ,
14+ sender : Browser . runtime . MessageSender ,
15+ sendResponse : ( response : unknown ) => void ,
16+ ) {
17+ console . debug ( "Background - Message received:" , message , sender ) ;
18+
19+ if ( isScrapActiveTabMessage ( message ) ) {
20+ scrapActiveTab ( ) ;
21+ return ;
22+ } else if ( isScreenshotSenderTab ( message ) ) {
23+ screenshotSenderTab ( sender ) . then ( ( data ) => {
24+ sendResponse ( data ) ;
25+ } ) ;
26+ return true ;
2827 }
29- console . log ( "Background - registering listener" ) ;
30- browser . runtime . onMessage . addListener ( handleMessages ) ;
28+ }
29+ console . log ( "Background - registering listener" ) ;
30+ browser . runtime . onMessage . addListener ( handleMessages ) ;
3131} ) ;
3232
33-
3433async function postToBackend ( post : Post ) {
35- console . debug ( "Background - Posting post to backend" ) ;
36- await fetch ( "http://localhost:8000/ml/post" , {
37- method : "POST" ,
38- headers : {
39- "Content-Type" : "application/json"
40- } ,
41- body : JSON . stringify ( post )
42- } ) ;
34+ console . debug ( "Background - Posting post to backend" ) ;
35+ await fetch ( "http://localhost:8000/ml/post" , {
36+ method : "POST" ,
37+ headers : {
38+ "Content-Type" : "application/json" ,
39+ } ,
40+ body : JSON . stringify ( post ) ,
41+ } ) ;
4342}
4443
45-
4644async function scrapActiveTab ( ) {
47- const tab = await getCurrentTab ( ) ;
45+ const tab = await getCurrentTab ( ) ;
4846
49- if ( tab ) {
50- console . debug ( "Background - Scraping post from active tab " , {
51- tabId : tab . id ,
52- url : tab . url ,
53- } ) ;
54- const socialNetworkPost = await scrapPostFromTab ( tab ) ;
55- await postToBackend ( socialNetworkPost ) ;
56- console . debug (
57- "Background - storing post to local storage" ,
58- socialNetworkPost ,
59- ) ;
60- await storePost ( socialNetworkPost ) ;
61- }
47+ if ( tab ) {
48+ console . debug ( "Background - Scraping post from active tab " , {
49+ tabId : tab . id ,
50+ url : tab . url ,
51+ } ) ;
52+ const socialNetworkPost = await scrapPostFromTab ( tab ) ;
53+ await postToBackend ( socialNetworkPost ) ;
54+ console . debug (
55+ "Background - storing post to local storage" ,
56+ socialNetworkPost ,
57+ ) ;
58+ await storePost ( socialNetworkPost ) ;
59+ }
6260}
0 commit comments