toolkit/devtools/gcli/commands/pagemod.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 const { Cc, Ci, Cu } = require("chrome");
michael@0 8 const gcli = require("gcli/index");
michael@0 9
michael@0 10 exports.items = [
michael@0 11 {
michael@0 12 name: "pagemod",
michael@0 13 description: gcli.lookup("pagemodDesc"),
michael@0 14 },
michael@0 15 {
michael@0 16 name: "pagemod replace",
michael@0 17 description: gcli.lookup("pagemodReplaceDesc"),
michael@0 18 params: [
michael@0 19 {
michael@0 20 name: "search",
michael@0 21 type: "string",
michael@0 22 description: gcli.lookup("pagemodReplaceSearchDesc"),
michael@0 23 },
michael@0 24 {
michael@0 25 name: "replace",
michael@0 26 type: "string",
michael@0 27 description: gcli.lookup("pagemodReplaceReplaceDesc"),
michael@0 28 },
michael@0 29 {
michael@0 30 name: "ignoreCase",
michael@0 31 type: "boolean",
michael@0 32 description: gcli.lookup("pagemodReplaceIgnoreCaseDesc"),
michael@0 33 },
michael@0 34 {
michael@0 35 name: "selector",
michael@0 36 type: "string",
michael@0 37 description: gcli.lookup("pagemodReplaceSelectorDesc"),
michael@0 38 defaultValue: "*:not(script):not(style):not(embed):not(object):not(frame):not(iframe):not(frameset)",
michael@0 39 },
michael@0 40 {
michael@0 41 name: "root",
michael@0 42 type: "node",
michael@0 43 description: gcli.lookup("pagemodReplaceRootDesc"),
michael@0 44 defaultValue: null,
michael@0 45 },
michael@0 46 {
michael@0 47 name: "attrOnly",
michael@0 48 type: "boolean",
michael@0 49 description: gcli.lookup("pagemodReplaceAttrOnlyDesc"),
michael@0 50 },
michael@0 51 {
michael@0 52 name: "contentOnly",
michael@0 53 type: "boolean",
michael@0 54 description: gcli.lookup("pagemodReplaceContentOnlyDesc"),
michael@0 55 },
michael@0 56 {
michael@0 57 name: "attributes",
michael@0 58 type: "string",
michael@0 59 description: gcli.lookup("pagemodReplaceAttributesDesc"),
michael@0 60 defaultValue: null,
michael@0 61 },
michael@0 62 ],
michael@0 63 // Make a given string safe to use in a regular expression.
michael@0 64 escapeRegex: function(aString) {
michael@0 65 return aString.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
michael@0 66 },
michael@0 67 exec: function(args, context) {
michael@0 68 let searchTextNodes = !args.attrOnly;
michael@0 69 let searchAttributes = !args.contentOnly;
michael@0 70 let regexOptions = args.ignoreCase ? "ig" : "g";
michael@0 71 let search = new RegExp(this.escapeRegex(args.search), regexOptions);
michael@0 72 let attributeRegex = null;
michael@0 73 if (args.attributes) {
michael@0 74 attributeRegex = new RegExp(args.attributes, regexOptions);
michael@0 75 }
michael@0 76
michael@0 77 let root = args.root || context.environment.document;
michael@0 78 let elements = root.querySelectorAll(args.selector);
michael@0 79 elements = Array.prototype.slice.call(elements);
michael@0 80
michael@0 81 let replacedTextNodes = 0;
michael@0 82 let replacedAttributes = 0;
michael@0 83
michael@0 84 function replaceAttribute() {
michael@0 85 replacedAttributes++;
michael@0 86 return args.replace;
michael@0 87 }
michael@0 88 function replaceTextNode() {
michael@0 89 replacedTextNodes++;
michael@0 90 return args.replace;
michael@0 91 }
michael@0 92
michael@0 93 for (let i = 0; i < elements.length; i++) {
michael@0 94 let element = elements[i];
michael@0 95 if (searchTextNodes) {
michael@0 96 for (let y = 0; y < element.childNodes.length; y++) {
michael@0 97 let node = element.childNodes[y];
michael@0 98 if (node.nodeType == node.TEXT_NODE) {
michael@0 99 node.textContent = node.textContent.replace(search, replaceTextNode);
michael@0 100 }
michael@0 101 }
michael@0 102 }
michael@0 103
michael@0 104 if (searchAttributes) {
michael@0 105 if (!element.attributes) {
michael@0 106 continue;
michael@0 107 }
michael@0 108 for (let y = 0; y < element.attributes.length; y++) {
michael@0 109 let attr = element.attributes[y];
michael@0 110 if (!attributeRegex || attributeRegex.test(attr.name)) {
michael@0 111 attr.value = attr.value.replace(search, replaceAttribute);
michael@0 112 }
michael@0 113 }
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 return gcli.lookupFormat("pagemodReplaceResult",
michael@0 118 [elements.length, replacedTextNodes,
michael@0 119 replacedAttributes]);
michael@0 120 }
michael@0 121 },
michael@0 122 {
michael@0 123 name: "pagemod remove",
michael@0 124 description: gcli.lookup("pagemodRemoveDesc"),
michael@0 125 },
michael@0 126 {
michael@0 127 name: "pagemod remove element",
michael@0 128 description: gcli.lookup("pagemodRemoveElementDesc"),
michael@0 129 params: [
michael@0 130 {
michael@0 131 name: "search",
michael@0 132 type: "string",
michael@0 133 description: gcli.lookup("pagemodRemoveElementSearchDesc"),
michael@0 134 },
michael@0 135 {
michael@0 136 name: "root",
michael@0 137 type: "node",
michael@0 138 description: gcli.lookup("pagemodRemoveElementRootDesc"),
michael@0 139 defaultValue: null,
michael@0 140 },
michael@0 141 {
michael@0 142 name: "stripOnly",
michael@0 143 type: "boolean",
michael@0 144 description: gcli.lookup("pagemodRemoveElementStripOnlyDesc"),
michael@0 145 },
michael@0 146 {
michael@0 147 name: "ifEmptyOnly",
michael@0 148 type: "boolean",
michael@0 149 description: gcli.lookup("pagemodRemoveElementIfEmptyOnlyDesc"),
michael@0 150 },
michael@0 151 ],
michael@0 152 exec: function(args, context) {
michael@0 153 let root = args.root || context.environment.document;
michael@0 154 let elements = Array.prototype.slice.call(root.querySelectorAll(args.search));
michael@0 155
michael@0 156 let removed = 0;
michael@0 157 for (let i = 0; i < elements.length; i++) {
michael@0 158 let element = elements[i];
michael@0 159 let parentNode = element.parentNode;
michael@0 160 if (!parentNode || !element.removeChild) {
michael@0 161 continue;
michael@0 162 }
michael@0 163 if (args.stripOnly) {
michael@0 164 while (element.hasChildNodes()) {
michael@0 165 parentNode.insertBefore(element.childNodes[0], element);
michael@0 166 }
michael@0 167 }
michael@0 168 if (!args.ifEmptyOnly || !element.hasChildNodes()) {
michael@0 169 element.parentNode.removeChild(element);
michael@0 170 removed++;
michael@0 171 }
michael@0 172 }
michael@0 173
michael@0 174 return gcli.lookupFormat("pagemodRemoveElementResultMatchedAndRemovedElements",
michael@0 175 [elements.length, removed]);
michael@0 176 }
michael@0 177 },
michael@0 178 {
michael@0 179 name: "pagemod remove attribute",
michael@0 180 description: gcli.lookup("pagemodRemoveAttributeDesc"),
michael@0 181 params: [
michael@0 182 {
michael@0 183 name: "searchAttributes",
michael@0 184 type: "string",
michael@0 185 description: gcli.lookup("pagemodRemoveAttributeSearchAttributesDesc"),
michael@0 186 },
michael@0 187 {
michael@0 188 name: "searchElements",
michael@0 189 type: "string",
michael@0 190 description: gcli.lookup("pagemodRemoveAttributeSearchElementsDesc"),
michael@0 191 },
michael@0 192 {
michael@0 193 name: "root",
michael@0 194 type: "node",
michael@0 195 description: gcli.lookup("pagemodRemoveAttributeRootDesc"),
michael@0 196 defaultValue: null,
michael@0 197 },
michael@0 198 {
michael@0 199 name: "ignoreCase",
michael@0 200 type: "boolean",
michael@0 201 description: gcli.lookup("pagemodRemoveAttributeIgnoreCaseDesc"),
michael@0 202 },
michael@0 203 ],
michael@0 204 exec: function(args, context) {
michael@0 205 let root = args.root || context.environment.document;
michael@0 206 let regexOptions = args.ignoreCase ? "ig" : "g";
michael@0 207 let attributeRegex = new RegExp(args.searchAttributes, regexOptions);
michael@0 208 let elements = root.querySelectorAll(args.searchElements);
michael@0 209 elements = Array.prototype.slice.call(elements);
michael@0 210
michael@0 211 let removed = 0;
michael@0 212 for (let i = 0; i < elements.length; i++) {
michael@0 213 let element = elements[i];
michael@0 214 if (!element.attributes) {
michael@0 215 continue;
michael@0 216 }
michael@0 217
michael@0 218 var attrs = Array.prototype.slice.call(element.attributes);
michael@0 219 for (let y = 0; y < attrs.length; y++) {
michael@0 220 let attr = attrs[y];
michael@0 221 if (attributeRegex.test(attr.name)) {
michael@0 222 element.removeAttribute(attr.name);
michael@0 223 removed++;
michael@0 224 }
michael@0 225 }
michael@0 226 }
michael@0 227
michael@0 228 return gcli.lookupFormat("pagemodRemoveAttributeResult",
michael@0 229 [elements.length, removed]);
michael@0 230 }
michael@0 231 },
michael@0 232 // This command allows the user to export the page to HTML after DOM changes
michael@0 233 {
michael@0 234 name: "export",
michael@0 235 description: gcli.lookup("exportDesc"),
michael@0 236 },
michael@0 237 {
michael@0 238 name: "export html",
michael@0 239 description: gcli.lookup("exportHtmlDesc"),
michael@0 240 params: [
michael@0 241 {
michael@0 242 name: "destination",
michael@0 243 type: {
michael@0 244 name: "selection",
michael@0 245 data: [ "window", "stdout", "clipboard" ]
michael@0 246 },
michael@0 247 defaultValue: "window"
michael@0 248 }
michael@0 249 ],
michael@0 250 exec: function(args, context) {
michael@0 251 let html = context.environment.document.documentElement.outerHTML;
michael@0 252 if (args.destination === "stdout") {
michael@0 253 return html;
michael@0 254 }
michael@0 255
michael@0 256 if (args.desination === "clipboard") {
michael@0 257 let clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"]
michael@0 258 .getService(Ci.nsIClipboardHelper);
michael@0 259 clipboard.copyString(url);
michael@0 260 return '';
michael@0 261 }
michael@0 262
michael@0 263 let url = "data:text/plain;charset=utf8," + encodeURIComponent(html);
michael@0 264 context.environment.window.open(url);
michael@0 265 return '';
michael@0 266 }
michael@0 267 }
michael@0 268 ];

mercurial