-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
118 lines (107 loc) · 3.13 KB
/
background.js
File metadata and controls
118 lines (107 loc) · 3.13 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Listens to messages from content scripts and panel scripts using long-running
* ports. Directs messages between them appropriately. Messages flow from
* in-page to contet script to background page to panel and back.
*
* @since 0.1.0
* @package DFPeep
* @copyright 2017 David Green <https://davidrg.com>
* @license MIT
*/
/* global chrome */
var contentPorts = {},
panelPorts = {},
debug = 1; // If 1 then enable console logs.
chrome.runtime.onConnect.addListener( function( port ) {
if ( 'DFPeepFromContent' !== port.name ) {
return;
}
var tabId = port.sender.tab.id;
contentPorts[ tabId ] = port;
if ( debug ) {
console.log('connected to content script' );
console.log( port );
}
port.postMessage( {message: 'message back from new port with background page'} );
var extensionListener = function( message, sender, sendResponse ) {
if ( debug ) {
console.log( 'background page received:' );
console.log( sender );
console.log( message );
}
if ( panelPorts[ sender.sender.tab.id ] ) {
panelPorts[ sender.sender.tab.id ].postMessage( message );
}
};
// Listens to messages sent from the content
port.onMessage.addListener( extensionListener );
port.onDisconnect.addListener( function( port ) {
port.onMessage.removeListener( extensionListener );
var tabId = port.sender.tab.id;
if ( contentPorts[ tabId ] ) {
delete contentPorts[ tabId ];
}
} );
} );
chrome.extension.onConnect.addListener( function( port ) {
if ( 'DFPeepFromPanel' !== port.name ) {
return;
}
if ( debug ) {
console.log( port );
console.log( 'established connection with panel' );
}
chrome.tabs.query(
{ active: true, currentWindow: true },
function( tabs ) {
var currentTab = tabs[0];
panelPorts[ currentTab.id ] = port;
}
);
var extensionListener = function( message, sender, sendResponse ) {
// Sent from panel, pass along to content script.
if ( debug ) {
console.log( 'background page heard from panel' );
console.log( message );
}
var tabId;
if ( sender.sender.tab && sender.sender.tab.id ) {
tabId = sender.sender.tab.id;
if ( contentPorts[ tabId ] ) {
contentPorts[ tabId ].postMessage( message );
}
} else {
// NOTE: This may be a bad idea because if the panel passively sends
// a request on a timeout then the user may have changed to another
// tab, resulting in the messaging going the wrong page.
// Right now with what I need it should be fine.
chrome.tabs.query(
{ active: true, currentWindow: true },
function( tabs ) {
tabId = tabs[0].id;
if ( contentPorts[ tabId ] ) {
contentPorts[ tabId ].postMessage( message );
}
}
);
}
};
// Listens to messages sent from the panel
port.onMessage.addListener( extensionListener );
port.onDisconnect.addListener( function( port ) {
if ( debug ) {
console.log( 'disconnecting panel' );
console.log( port );
}
port.onMessage.removeListener( extensionListener );
chrome.tabs.query(
{ active: true, currentWindow: true },
function( tabs ) {
var currentTab = tabs[0];
if ( panelPorts[ currentTab.id ] ) {
delete panelPorts[ currentTab.id ];
}
}
);
} );
} );