-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
executable file
·75 lines (56 loc) · 1.72 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
chrome.runtime.onInstalled.addListener(function() {
chrome.storage.sync.set({number: 1}, function() {
console.log('Extention is installed');
});
});
chrome.browserAction.onClicked.addListener( addTabToList );
// contains the tabs ids when extention is clicked on perticular tab
let tabs_list = [];
function addTabToList(tab){
tabs_list.push( tab.id) ;
tabs_list = [ ...new Set(tabs_list) ];
let x = chrome.tabs.create({url: chrome.extension.getURL('chats.html')});
}
// contains all the messages
// structure
// message:{
// name ,
// timestamp
// message
// contains all messages from diffrent meets
var messages = [] ;
//listent the messages
chrome.runtime.onMessage.addListener(
( req , sender , sendResponse ) =>{
// add message to the messages list
console.log( " message listened " ) ;
console.log ( req)
console.log( req.type ) ;
console.log( " message ends " ) ;
if( req.type ==="ADD"){
req.tabid = sender.tab.id;
messages.push ( req) ;
}
else if( req.type==="CLEAR"){
let ok = 0 ;
while( !ok ) {
ok = 1 ;
for( var i = 0; i < messages.length; i++){
if ( messages[i].tabid === sender.tab.id) {
messages.splice(i, 1);
ok = 0 ;
break;
}
}
}
}
var port = chrome.runtime.connect({name: "Chat"});
port.postMessage({messages, type:"CHAT"});
}
);
// print all messages in new tab
var print = ()=>{
for ( var i in messages ){
console.log( i ) ;
}
}