Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 function generateFilter(request) | |
2 { | |
3 var filter = request.url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||"); | |
4 | |
5 if (request.type == "POPUP") | |
6 { | |
7 filter += "$popup"; | |
8 | |
9 if (request.url == "about:blank") | |
10 filter += ",domain=" + request.docDomain; | |
11 } | |
12 | |
13 return filter; | |
14 } | |
15 | |
16 function createActionButton(action, label, filter, port) | |
17 { | |
18 var button = document.createElement("span"); | |
19 | |
20 button.textContent = label; | |
21 button.classList.add("action"); | |
22 | |
23 button.addEventListener("click", function() | |
24 { | |
25 port.postMessage({action: action, filter: filter}); | |
26 }); | |
27 | |
28 return button; | |
29 } | |
30 | |
31 function createRecord(request, filter, port) | |
32 { | |
kzar
2015/01/05 17:30:58
This function seems quite ugly, is there not a way
Sebastian Noack
2015/01/05 19:33:59
We don't use a template system. And I rather don't
| |
33 var row = document.createElement("tr"); | |
34 row.dataset.type = request.type; | |
35 var requestCell = document.createElement("td"); | |
36 row.appendChild(requestCell); | |
37 var requestWrapper = document.createElement("div"); | |
38 requestWrapper.classList.add("request-wrapper"); | |
39 requestCell.appendChild(requestWrapper); | |
40 var urlElement = document.createElement("div"); | |
41 urlElement.classList.add("url"); | |
42 urlElement.innerHTML = " "; | |
43 requestWrapper.appendChild(urlElement); | |
44 var domainElement = document.createElement("div"); | |
45 domainElement.classList.add("domain"); | |
46 domainElement.textContent = request.docDomain; | |
47 requestWrapper.appendChild(domainElement); | |
48 var typeElement = document.createElement("div"); | |
49 typeElement.classList.add("type"); | |
50 typeElement.textContent = request.type; | |
51 requestWrapper.appendChild(typeElement); | |
52 var filterCell = document.createElement("td"); | |
53 row.appendChild(filterCell); | |
54 var actionWrapper = document.createElement("div"); | |
55 actionWrapper.classList.add("action-wrapper"); | |
56 filterCell.appendChild(actionWrapper); | |
57 var filterWrapper = document.createElement("div"); | |
58 filterWrapper.classList.add("filter-wrapper"); | |
59 actionWrapper.appendChild(filterWrapper); | |
60 var filterElement = document.createElement("div"); | |
61 filterElement.innerHTML = " "; | |
62 filterElement.classList.add("filter"); | |
63 filterWrapper.appendChild(filterElement); | |
64 var originElement = document.createElement("div"); | |
65 originElement.innerHTML = " "; | |
66 originElement.classList.add("origin"); | |
67 filterWrapper.appendChild(originElement); | |
68 | |
69 if (request.url) | |
70 { | |
71 urlElement.textContent = request.url; | |
72 | |
73 if ("openResource" in chrome.devtools.panels && request.type != "POPUP") | |
74 { | |
75 urlElement.classList.add("resourceLink"); | |
76 urlElement.addEventListener("click", function() | |
77 { | |
78 chrome.devtools.panels.openResource(request.url); | |
79 }); | |
80 } | |
81 } | |
82 | |
83 if (filter) | |
84 { | |
85 filterElement.textContent = filter.text; | |
86 row.dataset.state = filter.whitelisted ? "whitelisted" : "blocked"; | |
87 | |
88 if (filter.subscription) | |
89 originElement.textContent = filter.subscription; | |
90 else | |
91 { | |
92 if (filter.userDefined) | |
93 originElement.textContent = "user-defined"; | |
94 else | |
95 originElement.textContent = "unnamed subscription"; | |
96 | |
97 originElement.classList.add("unnamed"); | |
98 } | |
99 | |
100 if (!filter.whitelisted && request.type != "ELEMHIDE") | |
101 actionWrapper.appendChild(createActionButton("add", "Whitelist request", " @@" + generateFilter(request), port)); | |
102 | |
103 if (filter.userDefined) | |
104 actionWrapper.appendChild(createActionButton("remove", "Remove filter", fi lter.text, port)); | |
105 } | |
106 else | |
107 actionWrapper.appendChild(createActionButton("add", "Block request", generat eFilter(request), port)); | |
108 | |
109 return row; | |
110 } | |
111 | |
112 document.addEventListener("DOMContentLoaded", function() | |
113 { | |
114 var port = chrome.runtime.connect({name: "devtools-" + chrome.devtools.inspect edWindow.tabId}); | |
115 var container = document.getElementById("items"); | |
116 var table = container.getElementsByTagName("table")[0]; | |
117 | |
118 port.onMessage.addListener(function(msg) | |
119 { | |
120 switch (msg.type) | |
121 { | |
122 case "add-record": | |
123 table.appendChild(createRecord(msg.request, msg.filter, port)); | |
124 break; | |
125 | |
126 case "update-record": | |
127 var oldRow = table.getElementsByTagName("tr")[msg.index]; | |
128 var newRow = createRecord(msg.request, msg.filter, port); | |
129 oldRow.parentNode.replaceChild(newRow, oldRow); | |
130 newRow.classList.add("changed"); | |
131 container.classList.add("has-changes"); | |
132 break; | |
133 | |
134 case "remove-record": | |
135 var row = table.getElementsByTagName("tr")[msg.index]; | |
136 row.parentNode.removeChild(row); | |
137 container.classList.add("has-changes"); | |
138 break; | |
139 | |
140 case "reset": | |
141 table.innerHTML = ""; | |
142 container.classList.remove("has-changes"); | |
143 break; | |
144 } | |
145 }); | |
146 | |
147 document.getElementById("reload").addEventListener("click", function() | |
148 { | |
149 chrome.devtools.inspectedWindow.reload(); | |
150 }); | |
151 | |
152 document.getElementById("filter-state").addEventListener("change", function(ev ent) | |
153 { | |
154 container.dataset.filterState = event.target.value; | |
155 }); | |
156 | |
157 document.getElementById("filter-type").addEventListener("change", function(eve nt) | |
158 { | |
159 container.dataset.filterType = event.target.value; | |
160 }); | |
161 }); | |
OLD | NEW |