Index: devtools-panel.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/devtools-panel.js |
@@ -0,0 +1,188 @@ |
+/* |
+ * This file is part of Adblock Plus <http://adblockplus.org/>, |
+ * Copyright (C) 2006-2015 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+function generateFilter(request) |
+{ |
+ var filter = request.url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||"); |
+ |
+ if (request.type == "POPUP") |
+ { |
+ filter += "$popup"; |
+ |
+ if (request.url == "about:blank") |
+ filter += ",domain=" + request.docDomain; |
+ } |
+ |
+ return filter; |
+} |
+ |
+function createActionButton(action, label, filter, port) |
+{ |
+ var button = document.createElement("span"); |
+ |
+ button.textContent = label; |
+ button.classList.add("action"); |
+ |
+ button.addEventListener("click", function() |
+ { |
+ port.postMessage({action: action, filter: filter}); |
+ }); |
+ |
+ return button; |
+} |
+ |
+function createRecord(request, filter, port) |
Thomas Greiner
2015/01/12 10:37:54
Seems like HTML templates is what you'd want to us
Sebastian Noack
2015/01/12 11:55:07
Sounds good. I will look into it.
|
+{ |
+ var row = document.createElement("tr"); |
+ row.dataset.type = request.type; |
+ |
+ var requestCell = document.createElement("td"); |
+ row.appendChild(requestCell); |
+ |
+ var requestWrapper = document.createElement("div"); |
+ requestWrapper.classList.add("request-wrapper"); |
+ requestCell.appendChild(requestWrapper); |
+ |
+ var urlElement = document.createElement("div"); |
+ urlElement.classList.add("url"); |
+ urlElement.innerHTML = " "; |
Thomas Greiner
2015/01/12 10:37:54
I assume you do this to ensure a minimum height fo
Sebastian Noack
2015/01/12 11:55:07
Doesn't seem to do the trick when using flexbox.
Thomas Greiner
2015/01/16 18:17:21
That's strange because it works for me when emptyi
Sebastian Noack
2015/02/04 14:17:29
I don't recall anymore what the particular problem
Thomas Greiner
2015/03/13 11:11:05
Assuring that the element has a minimum height is
Sebastian Noack
2015/03/13 13:07:38
I don't want a minimum height, I want each line to
|
+ requestWrapper.appendChild(urlElement); |
+ |
+ var domainElement = document.createElement("div"); |
+ domainElement.classList.add("domain"); |
+ domainElement.textContent = request.docDomain; |
+ requestWrapper.appendChild(domainElement); |
+ |
+ var typeElement = document.createElement("div"); |
+ typeElement.classList.add("type"); |
+ typeElement.textContent = request.type; |
+ requestWrapper.appendChild(typeElement); |
+ |
+ var filterCell = document.createElement("td"); |
+ row.appendChild(filterCell); |
+ |
+ var actionWrapper = document.createElement("div"); |
+ actionWrapper.classList.add("action-wrapper"); |
+ filterCell.appendChild(actionWrapper); |
+ |
+ var filterWrapper = document.createElement("div"); |
+ filterWrapper.classList.add("filter-wrapper"); |
+ actionWrapper.appendChild(filterWrapper); |
+ |
+ var filterElement = document.createElement("div"); |
+ filterElement.innerHTML = " "; |
+ filterElement.classList.add("filter"); |
+ filterWrapper.appendChild(filterElement); |
+ |
+ var originElement = document.createElement("div"); |
+ originElement.innerHTML = " "; |
+ originElement.classList.add("origin"); |
+ filterWrapper.appendChild(originElement); |
+ |
+ if (request.url) |
Thomas Greiner
2015/01/12 10:37:54
Which requests don't have URLs associated to them?
Sebastian Noack
2015/01/12 11:55:07
Element hiding filter matches.
|
+ { |
+ urlElement.textContent = request.url; |
+ |
+ if ("openResource" in ext.devtools.panels && request.type != "POPUP") |
Thomas Greiner
2015/01/12 10:37:54
Why would `ext.devtools.panels.openResource` not b
Sebastian Noack
2015/01/12 11:55:07
It's new in Chrome 38.
|
+ { |
+ urlElement.classList.add("resourceLink"); |
+ urlElement.addEventListener("click", function() |
Thomas Greiner
2015/01/12 10:37:54
No need to add an event listener to each row indiv
Sebastian Noack
2015/01/12 11:55:07
I know. But then I'd need to double check whether
Thomas Greiner
2015/01/16 18:17:21
I'd agree if `urlElement` had child elements that
Sebastian Noack
2015/02/04 14:17:29
I'd still need to check whether ext.devtools.panel
Thomas Greiner
2015/03/13 11:11:05
That makes sense. Separating the logic from the vi
|
+ { |
+ ext.devtools.panels.openResource(request.url); |
+ }); |
+ } |
+ } |
+ |
+ if (filter) |
+ { |
+ filterElement.textContent = filter.text; |
+ row.dataset.state = filter.whitelisted ? "whitelisted" : "blocked"; |
+ |
+ if (filter.subscription) |
+ originElement.textContent = filter.subscription; |
+ else |
+ { |
+ if (filter.userDefined) |
+ originElement.textContent = "user-defined"; |
+ else |
+ originElement.textContent = "unnamed subscription"; |
+ |
+ originElement.classList.add("unnamed"); |
+ } |
+ |
+ if (!filter.whitelisted && request.type != "ELEMHIDE") |
+ actionWrapper.appendChild(createActionButton("add", "Whitelist request", "@@" + generateFilter(request), port)); |
+ |
+ if (filter.userDefined) |
+ actionWrapper.appendChild(createActionButton("remove", "Remove filter", filter.text, port)); |
+ } |
+ else |
+ actionWrapper.appendChild(createActionButton("add", "Block request", generateFilter(request), port)); |
+ |
+ return row; |
+} |
+ |
+document.addEventListener("DOMContentLoaded", function() |
+{ |
+ var port = ext.devtools.connect(); |
+ var container = document.getElementById("items"); |
+ var table = container.getElementsByTagName("table")[0]; |
+ |
+ port.onMessage.addListener(function(msg) |
+ { |
+ switch (msg.type) |
+ { |
+ case "add-record": |
+ table.appendChild(createRecord(msg.request, msg.filter, port)); |
+ break; |
+ |
+ case "update-record": |
+ var oldRow = table.getElementsByTagName("tr")[msg.index]; |
+ var newRow = createRecord(msg.request, msg.filter, port); |
+ oldRow.parentNode.replaceChild(newRow, oldRow); |
+ newRow.classList.add("changed"); |
+ container.classList.add("has-changes"); |
+ break; |
+ |
+ case "remove-record": |
+ var row = table.getElementsByTagName("tr")[msg.index]; |
+ row.parentNode.removeChild(row); |
+ container.classList.add("has-changes"); |
+ break; |
+ |
+ case "reset": |
+ table.innerHTML = ""; |
+ container.classList.remove("has-changes"); |
+ break; |
+ } |
+ }); |
+ |
+ document.getElementById("reload").addEventListener("click", function() |
+ { |
+ ext.devtools.inspectedWindow.reload(); |
+ }); |
+ |
+ document.getElementById("filter-state").addEventListener("change", function(event) |
+ { |
+ container.dataset.filterState = event.target.value; |
+ }); |
+ |
+ document.getElementById("filter-type").addEventListener("change", function(event) |
+ { |
+ container.dataset.filterType = event.target.value; |
+ }); |
+}); |