OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 function generateFilter(request) |
| 19 { |
| 20 var filter = request.url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||"); |
| 21 |
| 22 if (request.type == "POPUP") |
| 23 { |
| 24 filter += "$popup"; |
| 25 |
| 26 if (request.url == "about:blank") |
| 27 filter += ",domain=" + request.docDomain; |
| 28 } |
| 29 |
| 30 return filter; |
| 31 } |
| 32 |
| 33 function createActionButton(action, label, filter, port) |
| 34 { |
| 35 var button = document.createElement("span"); |
| 36 |
| 37 button.textContent = label; |
| 38 button.classList.add("action"); |
| 39 |
| 40 button.addEventListener("click", function() |
| 41 { |
| 42 port.postMessage({action: action, filter: filter}); |
| 43 }); |
| 44 |
| 45 return button; |
| 46 } |
| 47 |
| 48 function createRecord(request, filter, port, template) |
| 49 { |
| 50 var row = document.importNode(template, true); |
| 51 |
| 52 row.querySelector(".domain").textContent = request.docDomain; |
| 53 row.querySelector(".type" ).textContent = request.type; |
| 54 |
| 55 var urlElement = row.querySelector(".url"); |
| 56 var actionWrapper = row.querySelector(".action-wrapper"); |
| 57 |
| 58 if (request.url) |
| 59 { |
| 60 urlElement.textContent = request.url; |
| 61 |
| 62 if ("openResource" in ext.devtools.panels && request.type != "POPUP") |
| 63 { |
| 64 urlElement.classList.add("resourceLink"); |
| 65 urlElement.addEventListener("click", function() |
| 66 { |
| 67 ext.devtools.panels.openResource(request.url); |
| 68 }); |
| 69 } |
| 70 } |
| 71 |
| 72 if (filter) |
| 73 { |
| 74 var filterElement = row.querySelector(".filter"); |
| 75 var originElement = row.querySelector(".origin"); |
| 76 |
| 77 filterElement.textContent = filter.text; |
| 78 row.dataset.state = filter.whitelisted ? "whitelisted" : "blocked"; |
| 79 |
| 80 if (filter.subscription) |
| 81 originElement.textContent = filter.subscription; |
| 82 else |
| 83 { |
| 84 if (filter.userDefined) |
| 85 originElement.textContent = "user-defined"; |
| 86 else |
| 87 originElement.textContent = "unnamed subscription"; |
| 88 |
| 89 originElement.classList.add("unnamed"); |
| 90 } |
| 91 |
| 92 if (!filter.whitelisted && request.type != "ELEMHIDE") |
| 93 actionWrapper.appendChild(createActionButton("add", "Add exception", "@@"
+ generateFilter(request), port)); |
| 94 |
| 95 if (filter.userDefined) |
| 96 actionWrapper.appendChild(createActionButton("remove", "Remove rule", filt
er.text, port)); |
| 97 } |
| 98 else |
| 99 actionWrapper.appendChild(createActionButton("add", "Block item", generateFi
lter(request), port)); |
| 100 |
| 101 return row; |
| 102 } |
| 103 |
| 104 document.addEventListener("DOMContentLoaded", function() |
| 105 { |
| 106 var port = ext.devtools.connect(); |
| 107 |
| 108 var container = document.getElementById("items"); |
| 109 var table = container.querySelector("tbody"); |
| 110 var template = document.querySelector("template").content.firstElementChild; |
| 111 |
| 112 port.onMessage.addListener(function(msg) |
| 113 { |
| 114 switch (msg.type) |
| 115 { |
| 116 case "add-record": |
| 117 table.appendChild(createRecord(msg.request, msg.filter, port, template))
; |
| 118 break; |
| 119 |
| 120 case "update-record": |
| 121 var oldRow = table.getElementsByTagName("tr")[msg.index]; |
| 122 var newRow = createRecord(msg.request, msg.filter, port, template); |
| 123 oldRow.parentNode.replaceChild(newRow, oldRow); |
| 124 newRow.classList.add("changed"); |
| 125 container.classList.add("has-changes"); |
| 126 break; |
| 127 |
| 128 case "remove-record": |
| 129 var row = table.getElementsByTagName("tr")[msg.index]; |
| 130 row.parentNode.removeChild(row); |
| 131 container.classList.add("has-changes"); |
| 132 break; |
| 133 |
| 134 case "reset": |
| 135 table.innerHTML = ""; |
| 136 container.classList.remove("has-changes"); |
| 137 break; |
| 138 } |
| 139 }); |
| 140 |
| 141 document.getElementById("reload").addEventListener("click", function() |
| 142 { |
| 143 ext.devtools.inspectedWindow.reload(); |
| 144 }); |
| 145 |
| 146 document.getElementById("filter-state").addEventListener("change", function(ev
ent) |
| 147 { |
| 148 container.dataset.filterState = event.target.value; |
| 149 }); |
| 150 |
| 151 document.getElementById("filter-type").addEventListener("change", function(eve
nt) |
| 152 { |
| 153 container.dataset.filterType = event.target.value; |
| 154 }); |
| 155 }); |
OLD | NEW |