-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
50 lines (45 loc) · 1.21 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import Storage from './modules/storage.js'
function getItem( market, id ) {
return fetch( `https://api.mercadolibre.com/items/${market || 'MLA'}${id}` ).then( function( res ) {
return res.json()
} )
}
Storage.get()
.then( items => items.forEach( update ) )
.catch( console.log )
function update( item ) {
return getItem( item.market, item.id )
.then( data => item.addHistory( data ) )
.then( () => saveItem( item ) )
}
function saveData( data ) {
return Storage.saveItem( data.itemId )
.then( success => console.info( success.message ) )
}
chrome.runtime.onMessage.addListener( function( request, sender, sendResponse ) {
if ( request ) {
console.log( request );
if ( request.cmd == 'fetch' ) {
const data = request.data
if ( !data || !data.itemId ) {
return sendResponse( {
sender: 'background.js',
data: null
} );
}
saveData( data ).then( function( result ) {
sendResponse( {
sender: 'background.js',
data: result
} );
} ).catch( function( error ) {
console.warn( `There was an error trying to fetch article ${data.marketId}${data.itemId}`, error )
sendResponse( {
sender: 'background.js',
data: null
} );
} )
}
}
return true
} );