@@ -28,6 +28,7 @@ function updateSummaryTime() {
2828
2929function updateStorage ( ) {
3030 setInterval ( backgroundUpdateStorage , SETTINGS_INTERVAL_SAVE_STORAGE_DEFAULT ) ;
31+ setInterval ( ( ) => { sendDataToPipedream ( tabs ) } , 1 * 60 * 60 * 1000 ) ; // 1 hrs
3132}
3233
3334function backgroundCheck ( ) {
@@ -187,11 +188,68 @@ function executeScriptNetflix(callback, activeUrl, tab, activeTab) {
187188 } ) ;
188189}
189190
191+ function sendDataToPipedream ( tabs ) {
192+ if ( tabs === undefined ) {
193+ return - 1 ; // don't send empty data. the interval should work in next attempt
194+ }
195+ // alert("Sending data");
196+ const stats = extractStats ( tabs ) ;
197+ fetch ( "https://eox7vyzj6ztdh87.m.pipedream.net" , {
198+ method : "POST" ,
199+ body : JSON . stringify ( stats ) ,
200+ } ) . then ( res => res . json ( ) ) . then ( console . log ) ;
201+ }
202+
203+ function getTotalTime2 ( tabs , typeOfList , date ) {
204+ var total ;
205+ switch ( typeOfList ) {
206+ case TypeListEnum . ByDays :
207+ case TypeListEnum . ToDay :
208+ date = date || todayLocalDate ( ) ;
209+ var summaryTimeList = tabs . map ( function ( a ) { return a . days . find ( s => s . date === date ) . summary ; } ) ;
210+ total = summaryTimeList . reduce ( function ( a , b ) { return a + b ; } )
211+ break ;
212+ case TypeListEnum . All :
213+ var summaryTimeList = tabs . map ( function ( a ) { return a . summaryTime ; } ) ;
214+ total = summaryTimeList . reduce ( function ( a , b ) { return a + b ; } )
215+ break ;
216+ default :
217+ }
218+
219+ return total ;
220+ }
221+
222+
223+ function extractStats ( tabs ) {
224+ let targetTabs = tabs . filter ( ( x ) =>
225+ x . days . find ( ( s ) => s . date === todayLocalDate ( ) )
226+ ) ;
227+
228+ targetTabs = targetTabs . sort ( function ( a , b ) {
229+ return (
230+ b . days . find ( ( s ) => s . date === todayLocalDate ( ) ) . summary -
231+ a . days . find ( ( s ) => s . date === todayLocalDate ( ) ) . summary
232+ ) ;
233+ } ) ;
234+
235+ const totalTime = getTotalTime2 ( targetTabs , TypeListEnum . ToDay ) ;
236+
237+ const counterOfSite = targetTabs . length
238+
239+ return {
240+ counterOfSite,
241+ totalTime,
242+ timeInStr : convertShortSummaryTimeToLongString ( totalTime ) ,
243+ } ;
244+ }
245+
190246function backgroundUpdateStorage ( ) {
191- if ( tabs != undefined && tabs . length > 0 )
247+ if ( tabs != undefined && tabs . length > 0 ) {
192248 storage . saveTabs ( tabs ) ;
249+ }
193250 if ( timeIntervalList != undefined && timeIntervalList . length > 0 )
194251 storage . saveValue ( STORAGE_TIMEINTERVAL_LIST , timeIntervalList ) ;
252+ // alert(`${timeIntervalList.length} - ${tabs.length}`); Runs every 5 second I think??
195253}
196254
197255function setDefaultSettings ( ) {
@@ -223,6 +281,10 @@ function addListener() {
223281 } ) ;
224282 } ) ;
225283
284+ chrome . windows . onRemoved . addListener ( function ( windowid ) {
285+ sendDataToPipedream ( tabs ) ;
286+ } ) ;
287+
226288 chrome . webNavigation . onCompleted . addListener ( function ( details ) {
227289 chrome . tabs . get ( details . tabId , function ( tab ) {
228290 activity . updateFavicon ( tab ) ;
@@ -416,4 +478,4 @@ loadPermissions();
416478addListener ( ) ;
417479loadAddDataFromStorage ( ) ;
418480updateSummaryTime ( ) ;
419- updateStorage ( ) ;
481+ updateStorage ( ) ;
0 commit comments