forked from Kenshin/simpread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.js
More file actions
91 lines (82 loc) · 2.19 KB
/
watch.js
File metadata and controls
91 lines (82 loc) · 2.19 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
console.log( "=== simpread storage load ===" )
import * as msg from 'message';
import {br,browser}from 'browser';
const watcher = {
site : new Map(),
import : new Map(),
version: new Map(),
option : new Map(),
};
/**
* Message watcher push
*
* @param {string} type watcher object, incude: site
* @param {string} value watcher object state
*/
function message( type, value ) {
browser.runtime.sendMessage( msg.Add( msg.MESSAGE_ACTION.updated, { type, value } ));
}
/**
* Push watcher target
*
* @param {string} type watcher object, incude: site
* @param {string} value watcher object state
*/
function push( type, value ) {
getCurAllTabs( type );
}
/**
* Pull( remove ) watcher by tabid
*
* @param {string} tab id
*/
function pull( tabid ) {
Object.values( watcher ).forEach( item => item.delete( tabid ));
}
/**
* Lock
*
* @param {string} url
* @return {object} return wacher item, when url exist tabs status is lock( true ), else is unlock( false )
*/
function lock( url ) {
try {
return {
site : [ ...watcher.site.values() ].includes( url ),
import : [ ...watcher.import.values() ].includes( url ),
version: [ ...watcher.version.values()].includes( url ),
option : [ ...watcher.option.values() ].includes( url ),
};
} catch( error ) {
console.error( "watch.Lock has same failed, ", error );
return { site: false, import: false };
}
}
/**
* Verify
*
* @param {fucntion} callback watch.Lock() state, result
*/
function verify( callback ) {
!br.isFirefox() ?
browser.runtime.sendMessage( msg.Add( msg.MESSAGE_ACTION.save_verify, { url: window.location.href }), result => {
callback( result.site || result.import || result.version || result.option, result );
}) : callback( false );
}
/**
* Get current all tabs
*
* @param {string} @see wathc.Push()
*/
function getCurAllTabs( type ) {
browser.tabs.query( {}, result => {
result.forEach( tab => watcher[type].set( tab.id, tab.url ));
});
}
export {
message as SendMessage,
push as Push,
pull as Pull,
verify as Verify,
lock as Lock,
}