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 { |
| 33 var row = document.createElement("tr"); |
| 34 row.dataset.type = request.type; |
| 35 |
| 36 var requestCell = document.createElement("td"); |
| 37 row.appendChild(requestCell); |
| 38 |
| 39 var requestWrapper = document.createElement("div"); |
| 40 requestWrapper.classList.add("request-wrapper"); |
| 41 requestCell.appendChild(requestWrapper); |
| 42 |
| 43 var urlElement = document.createElement("div"); |
| 44 urlElement.classList.add("url"); |
| 45 urlElement.innerHTML = " "; |
| 46 requestWrapper.appendChild(urlElement); |
| 47 |
| 48 var domainElement = document.createElement("div"); |
| 49 domainElement.classList.add("domain"); |
| 50 domainElement.textContent = request.docDomain; |
| 51 requestWrapper.appendChild(domainElement); |
| 52 |
| 53 var typeElement = document.createElement("div"); |
| 54 typeElement.classList.add("type"); |
| 55 typeElement.textContent = request.type; |
| 56 requestWrapper.appendChild(typeElement); |
| 57 |
| 58 var filterCell = document.createElement("td"); |
| 59 row.appendChild(filterCell); |
| 60 |
| 61 var actionWrapper = document.createElement("div"); |
| 62 actionWrapper.classList.add("action-wrapper"); |
| 63 filterCell.appendChild(actionWrapper); |
| 64 |
| 65 var filterWrapper = document.createElement("div"); |
| 66 filterWrapper.classList.add("filter-wrapper"); |
| 67 actionWrapper.appendChild(filterWrapper); |
| 68 |
| 69 var filterElement = document.createElement("div"); |
| 70 filterElement.innerHTML = " "; |
| 71 filterElement.classList.add("filter"); |
| 72 filterWrapper.appendChild(filterElement); |
| 73 |
| 74 var originElement = document.createElement("div"); |
| 75 originElement.innerHTML = " "; |
| 76 originElement.classList.add("origin"); |
| 77 filterWrapper.appendChild(originElement); |
| 78 |
| 79 if (request.url) |
| 80 { |
| 81 urlElement.textContent = request.url; |
| 82 |
| 83 if ("openResource" in chrome.devtools.panels && request.type != "POPUP") |
| 84 { |
| 85 urlElement.classList.add("resourceLink"); |
| 86 urlElement.addEventListener("click", function() |
| 87 { |
| 88 chrome.devtools.panels.openResource(request.url); |
| 89 }); |
| 90 } |
| 91 } |
| 92 |
| 93 if (filter) |
| 94 { |
| 95 filterElement.textContent = filter.text; |
| 96 row.dataset.state = filter.whitelisted ? "whitelisted" : "blocked"; |
| 97 |
| 98 if (filter.subscription) |
| 99 originElement.textContent = filter.subscription; |
| 100 else |
| 101 { |
| 102 if (filter.userDefined) |
| 103 originElement.textContent = "user-defined"; |
| 104 else |
| 105 originElement.textContent = "unnamed subscription"; |
| 106 |
| 107 originElement.classList.add("unnamed"); |
| 108 } |
| 109 |
| 110 if (!filter.whitelisted && request.type != "ELEMHIDE") |
| 111 actionWrapper.appendChild(createActionButton("add", "Whitelist request", "
@@" + generateFilter(request), port)); |
| 112 |
| 113 if (filter.userDefined) |
| 114 actionWrapper.appendChild(createActionButton("remove", "Remove filter", fi
lter.text, port)); |
| 115 } |
| 116 else |
| 117 actionWrapper.appendChild(createActionButton("add", "Block request", generat
eFilter(request), port)); |
| 118 |
| 119 return row; |
| 120 } |
| 121 |
| 122 document.addEventListener("DOMContentLoaded", function() |
| 123 { |
| 124 var port = chrome.runtime.connect({name: "devtools-" + chrome.devtools.inspect
edWindow.tabId}); |
| 125 var container = document.getElementById("items"); |
| 126 var table = container.getElementsByTagName("table")[0]; |
| 127 |
| 128 port.onMessage.addListener(function(msg) |
| 129 { |
| 130 switch (msg.type) |
| 131 { |
| 132 case "add-record": |
| 133 table.appendChild(createRecord(msg.request, msg.filter, port)); |
| 134 break; |
| 135 |
| 136 case "update-record": |
| 137 var oldRow = table.getElementsByTagName("tr")[msg.index]; |
| 138 var newRow = createRecord(msg.request, msg.filter, port); |
| 139 oldRow.parentNode.replaceChild(newRow, oldRow); |
| 140 newRow.classList.add("changed"); |
| 141 container.classList.add("has-changes"); |
| 142 break; |
| 143 |
| 144 case "remove-record": |
| 145 var row = table.getElementsByTagName("tr")[msg.index]; |
| 146 row.parentNode.removeChild(row); |
| 147 container.classList.add("has-changes"); |
| 148 break; |
| 149 |
| 150 case "reset": |
| 151 table.innerHTML = ""; |
| 152 container.classList.remove("has-changes"); |
| 153 break; |
| 154 } |
| 155 }); |
| 156 |
| 157 document.getElementById("reload").addEventListener("click", function() |
| 158 { |
| 159 chrome.devtools.inspectedWindow.reload(); |
| 160 }); |
| 161 |
| 162 document.getElementById("filter-state").addEventListener("change", function(ev
ent) |
| 163 { |
| 164 container.dataset.filterState = event.target.value; |
| 165 }); |
| 166 |
| 167 document.getElementById("filter-type").addEventListener("change", function(eve
nt) |
| 168 { |
| 169 container.dataset.filterType = event.target.value; |
| 170 }); |
| 171 }); |
OLD | NEW |