|
1 |
| -chrome.tabs.onUpdated.addListener(function(tabid) |
| 1 | +chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) |
2 | 2 | {
|
3 |
| - chrome.tabs.getSelected(null, function(tab) |
| 3 | + // We only react on a complete load of a http(s) page, |
| 4 | + // only then we're sure the content.js is loaded. |
| 5 | + if (changeInfo.status !== "complete" || tab.url.indexOf("http") !== 0) |
4 | 6 | {
|
5 |
| - if (!localStorage) |
6 |
| - { |
7 |
| - return; |
8 |
| - } |
| 7 | + return; |
| 8 | + } |
| 9 | + |
| 10 | + // Prep some variables |
| 11 | + var sites = [], |
| 12 | + ideKey = "XDEBUG_ECLIPSE", |
| 13 | + match = false, |
| 14 | + domain; |
9 | 15 |
|
10 |
| - sites = localStorage["sites"]; |
11 |
| - if (!sites) |
| 16 | + // Check if localStorage is available and get the settings out of it |
| 17 | + if (localStorage) |
| 18 | + { |
| 19 | + if (localStorage["sites"]) |
12 | 20 | {
|
13 |
| - sites = "[]"; |
| 21 | + sites = JSON.parse(localStorage["sites"]); |
14 | 22 | }
|
15 |
| - sites = JSON.parse(sites); |
16 |
| - |
17 |
| - baseDomain = tab.url.match(/:\/\/(.[^\/]+)/)[1]; |
18 |
| - |
19 |
| - match = isValueInArray(sites, baseDomain); |
20 | 23 |
|
21 |
| - if (match || sites.length === 0) |
| 24 | + if (localStorage["xdebugIdeKey"]) |
22 | 25 | {
|
23 |
| - chrome.pageAction.show(tabid); |
24 |
| - chrome.tabs.getSelected(null, function(tab) |
25 |
| - { |
26 |
| - chrome.tabs.sendRequest( |
27 |
| - tab.id, |
28 |
| - { |
29 |
| - cmd: "status", |
30 |
| - idekey: localStorage["xdebugIdeKey"] |
31 |
| - }, |
32 |
| - function(response) |
33 |
| - { |
34 |
| - updateIcon(response.result, tabid); |
35 |
| - } |
36 |
| - ); |
37 |
| - }); |
| 26 | + ideKey = localStorage["xdebugIdeKey"]; |
38 | 27 | }
|
39 |
| - }); |
40 |
| -}); |
| 28 | + } |
41 | 29 |
|
42 |
| -chrome.pageAction.onClicked.addListener(function(tab) |
43 |
| -{ |
44 |
| - chrome.tabs.getSelected(null, function(tab) |
| 30 | + // Get the current domain out of the tab URL and check if it matches anything in the sites array |
| 31 | + domain = tab.url.match(/:\/\/(.[^\/]+)/)[1]; |
| 32 | + match = isValueInArray(sites, domain); |
| 33 | + |
| 34 | + // Check if we have a match or don't need to match at all |
| 35 | + if (match || sites.length === 0) |
45 | 36 | {
|
46 |
| - chrome.tabs.sendRequest( |
47 |
| - tab.id, |
| 37 | + // Show the pageAction |
| 38 | + chrome.pageAction.show(tabId); |
| 39 | + |
| 40 | + // Request the current status and update the icon accordingly |
| 41 | + chrome.tabs.sendMessage( |
| 42 | + tabId, |
48 | 43 | {
|
49 |
| - cmd: "toggle", idekey: localStorage["xdebugIdeKey"] |
| 44 | + cmd: "getStatus", |
| 45 | + idekey: ideKey |
50 | 46 | },
|
51 | 47 | function(response)
|
52 | 48 | {
|
53 |
| - updateIcon(response.result, tab.id); |
| 49 | + updateIcon(response.status, tabId); |
54 | 50 | }
|
55 | 51 | );
|
56 |
| - }); |
| 52 | + } |
57 | 53 | });
|
58 | 54 |
|
59 |
| -function updateIcon(status, tabid) |
| 55 | +function updateIcon(status, tabId) |
60 | 56 | {
|
| 57 | + // Figure the correct title/image with the given state |
| 58 | + var title = "Debugging, profiling & tracing disabled", |
| 59 | + image = "images/bug-gray.png"; |
| 60 | + |
61 | 61 | if (status == 1)
|
62 | 62 | {
|
63 |
| - chrome.pageAction.setTitle({ |
64 |
| - tabId: tabid, |
65 |
| - title: "Debugging enabled" |
66 |
| - }); |
67 |
| - |
68 |
| - chrome.pageAction.setIcon({ |
69 |
| - tabId: tabid, |
70 |
| - path: "images/bug.png" |
71 |
| - }); |
| 63 | + title = "Debugging enabled"; |
| 64 | + image = "images/bug.png"; |
72 | 65 | }
|
73 | 66 | else if (status == 2)
|
74 | 67 | {
|
75 |
| - chrome.pageAction.setTitle({ |
76 |
| - tabId: tabid, |
77 |
| - title: "Profiling enabled" |
78 |
| - }); |
79 |
| - |
80 |
| - chrome.pageAction.setIcon({ |
81 |
| - tabId: tabid, |
82 |
| - path: "images/clock.png" |
83 |
| - }); |
| 68 | + title = "Profiling enabled"; |
| 69 | + image = "images/clock.png"; |
84 | 70 | }
|
85 | 71 | else if (status == 3)
|
86 | 72 | {
|
87 |
| - chrome.pageAction.setTitle({ |
88 |
| - tabId: tabid, |
89 |
| - title: "Tracing enabled" |
90 |
| - }); |
91 |
| - |
92 |
| - chrome.pageAction.setIcon({ |
93 |
| - tabId: tabid, |
94 |
| - path: "images/script.png" |
95 |
| - }); |
| 73 | + title = "Tracing enabled"; |
| 74 | + image = "images/script.png"; |
96 | 75 | }
|
97 |
| - else |
98 |
| - { |
99 |
| - chrome.pageAction.setTitle({ |
100 |
| - tabId: tabid, |
101 |
| - title: "Debugging, profiling & tracing disabled" |
102 |
| - }); |
103 | 76 |
|
104 |
| - chrome.pageAction.setIcon({ |
105 |
| - tabId: tabid, |
106 |
| - path: "images/bug-gray.png" |
107 |
| - }); |
108 |
| - } |
| 77 | + // Update title |
| 78 | + chrome.pageAction.setTitle({ |
| 79 | + tabId: tabId, |
| 80 | + title: title |
| 81 | + }); |
| 82 | + |
| 83 | + // Update image |
| 84 | + chrome.pageAction.setIcon({ |
| 85 | + tabId: tabId, |
| 86 | + path: image |
| 87 | + }); |
109 | 88 | }
|
110 | 89 |
|
111 | 90 | function isValueInArray(arr, val)
|
112 | 91 | {
|
113 | 92 | for (i = 0; i < arr.length; i++)
|
114 | 93 | {
|
115 |
| - re = new RegExp(arr[i], "gi"); |
| 94 | + var re = new RegExp(arr[i], "gi"); |
116 | 95 | if (re.test(val))
|
117 | 96 | {
|
118 | 97 | return true;
|
|
0 commit comments