OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 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 "use strict"; |
| 19 |
| 20 function generateFilter(request) |
| 21 { |
| 22 var filter = request.url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||"); |
| 23 |
| 24 if (request.type == "POPUP") |
| 25 { |
| 26 filter += "$popup"; |
| 27 |
| 28 if (request.url == "about:blank") |
| 29 filter += ",domain=" + request.docDomain; |
| 30 } |
| 31 |
| 32 return filter; |
| 33 } |
| 34 |
| 35 function createActionButton(action, label, filter) |
| 36 { |
| 37 var button = document.createElement("span"); |
| 38 |
| 39 button.textContent = label; |
| 40 button.classList.add("action"); |
| 41 |
| 42 button.addEventListener("click", function() |
| 43 { |
| 44 ext.backgroundPage.sendMessage({ |
| 45 type: "filters." + action, |
| 46 text: filter |
| 47 }); |
| 48 }, false); |
| 49 |
| 50 return button; |
| 51 } |
| 52 |
| 53 function createRecord(request, filter, template) |
| 54 { |
| 55 var row = document.importNode(template, true); |
| 56 row.dataset.type = request.type; |
| 57 |
| 58 row.querySelector(".domain").textContent = request.docDomain; |
| 59 row.querySelector(".type").textContent = request.type; |
| 60 |
| 61 var urlElement = row.querySelector(".url"); |
| 62 var actionWrapper = row.querySelector(".action-wrapper"); |
| 63 |
| 64 if (request.url) |
| 65 { |
| 66 urlElement.textContent = request.url; |
| 67 |
| 68 if ("openResource" in ext.devtools.panels && request.type != "POPUP") |
| 69 { |
| 70 urlElement.classList.add("resourceLink"); |
| 71 urlElement.addEventListener("click", function() |
| 72 { |
| 73 ext.devtools.panels.openResource(request.url); |
| 74 }, false); |
| 75 } |
| 76 } |
| 77 |
| 78 if (filter) |
| 79 { |
| 80 var filterElement = row.querySelector(".filter"); |
| 81 var originElement = row.querySelector(".origin"); |
| 82 |
| 83 filterElement.textContent = filter.text; |
| 84 row.dataset.state = filter.whitelisted ? "whitelisted" : "blocked"; |
| 85 |
| 86 if (filter.subscription) |
| 87 originElement.textContent = filter.subscription; |
| 88 else |
| 89 { |
| 90 if (filter.userDefined) |
| 91 originElement.textContent = "user-defined"; |
| 92 else |
| 93 originElement.textContent = "unnamed subscription"; |
| 94 |
| 95 originElement.classList.add("unnamed"); |
| 96 } |
| 97 |
| 98 if (!filter.whitelisted && request.type != "ELEMHIDE") |
| 99 actionWrapper.appendChild(createActionButton("add", "Add exception", "@@"
+ generateFilter(request))); |
| 100 |
| 101 if (filter.userDefined) |
| 102 actionWrapper.appendChild(createActionButton("remove", "Remove rule", filt
er.text)); |
| 103 } |
| 104 else |
| 105 actionWrapper.appendChild(createActionButton("add", "Block item", generateFi
lter(request))); |
| 106 |
| 107 return row; |
| 108 } |
| 109 |
| 110 document.addEventListener("DOMContentLoaded", function() |
| 111 { |
| 112 var container = document.getElementById("items"); |
| 113 var table = container.querySelector("tbody"); |
| 114 var template = document.querySelector("template").content.firstElementChild; |
| 115 |
| 116 document.getElementById("reload").addEventListener("click", function() |
| 117 { |
| 118 ext.devtools.inspectedWindow.reload(); |
| 119 }, false); |
| 120 |
| 121 document.getElementById("filter-state").addEventListener("change", function(ev
ent) |
| 122 { |
| 123 container.dataset.filterState = event.target.value; |
| 124 }, false); |
| 125 |
| 126 document.getElementById("filter-type").addEventListener("change", function(eve
nt) |
| 127 { |
| 128 container.dataset.filterType = event.target.value; |
| 129 }, false); |
| 130 |
| 131 ext.onMessage.addListener(function(message) |
| 132 { |
| 133 switch (message.type) |
| 134 { |
| 135 case "add-record": |
| 136 table.appendChild(createRecord(message.request, message.filter, template
)); |
| 137 break; |
| 138 |
| 139 case "update-record": |
| 140 var oldRow = table.getElementsByTagName("tr")[message.index]; |
| 141 var newRow = createRecord(message.request, message.filter, template); |
| 142 oldRow.parentNode.replaceChild(newRow, oldRow); |
| 143 newRow.classList.add("changed"); |
| 144 container.classList.add("has-changes"); |
| 145 break; |
| 146 |
| 147 case "remove-record": |
| 148 var row = table.getElementsByTagName("tr")[message.index]; |
| 149 row.parentNode.removeChild(row); |
| 150 container.classList.add("has-changes"); |
| 151 break; |
| 152 |
| 153 case "reset": |
| 154 table.innerHTML = ""; |
| 155 container.classList.remove("has-changes"); |
| 156 break; |
| 157 } |
| 158 }); |
| 159 }, false); |
OLD | NEW |