forked from mak08/VRDashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
66 lines (56 loc) · 1.84 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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var version = "1.0";
var debuggeeTab;
var dashboardTab;
function onPageClicked (tab) {
debuggeeTab = tab;
chrome.debugger.attach({tabId:tab.id}, version, onAttach.bind(null, tab.id));
}
function checkForValidUrl (tabId, changeInfo, tabInfo) {
try {
if (tabInfo && tabInfo.url.indexOf('virtualregatta.com') >= 0) {
chrome.pageAction.show(tabId);
}
} catch (e) {
console.log("Tab is gone: " + tabId);
}
};
function onTabRemoved (tabId, removeInfo) {
if ( debuggeeTab && (tabId == debuggeeTab.id) ) {
try {
chrome.tabs.remove(dashboardTab.id);
} catch (e) {
console.log(JSON.stringify(e));
}
} else if (dashboardTab && (tabId == dashboardTab.id) ) {
try {
chrome.debugger.detach({
tabId: debuggeeTab.id
});
} catch (e) {
console.log(JSON.stringify(e));
}
}
}
function onAttach (tabId) {
if (chrome.runtime.lastError) {
alert(chrome.runtime.lastError.message);
} else {
chrome.tabs.create({url: "dashboard.html?" + tabId, active: false},
function (tab) {
dashboardTab = tab;
});
}
}
chrome.pageAction.onClicked.addListener( onPageClicked );
chrome.tabs.onUpdated.addListener( checkForValidUrl );
chrome.tabs.onRemoved.addListener( onTabRemoved );
// mostly useful for development - enable page action after
// ext reload
chrome.tabs.onActivated.addListener( function (activeInfo) {
chrome.tabs.get(activeInfo.tabId, function (tab) {
checkForValidUrl(activeInfo.tabId,null,tab);
});
});