/usr/lib/ruby/vendor_ruby/web_console/templates/console.js.erb is in ruby-web-console 2.2.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | /**
* Constructor for command storage.
* It uses localStorage if available. Otherwise fallback to normal JS array.
*/
function CommandStorage() {
this.previousCommands = [];
var previousCommandOffset = 0;
var hasLocalStorage = typeof window.localStorage !== 'undefined';
var STORAGE_KEY = "web_console_previous_commands";
var MAX_STORAGE = 100;
if (hasLocalStorage) {
this.previousCommands = JSON.parse(localStorage.getItem(STORAGE_KEY)) || [];
previousCommandOffset = this.previousCommands.length;
}
this.addCommand = function(command) {
previousCommandOffset = this.previousCommands.push(command);
if (previousCommandOffset > MAX_STORAGE) {
this.previousCommands.splice(0, 1);
previousCommandOffset = MAX_STORAGE;
}
if (hasLocalStorage) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(this.previousCommands));
}
};
this.navigate = function(offset) {
previousCommandOffset += offset;
if (previousCommandOffset < 0) {
previousCommandOffset = -1;
return null;
}
if (previousCommandOffset >= this.previousCommands.length) {
previousCommandOffset = this.previousCommands.length;
return null;
}
return this.previousCommands[previousCommandOffset];
}
}
// HTML strings for dynamic elements.
var consoleInnerHtml = <%= render_inlined_string '_inner_console_markup.html' %>;
var promptBoxHtml = <%= render_inlined_string '_prompt_box_markup.html' %>;
// CSS
var consoleStyleCss = <%= render_inlined_string 'style.css' %>;
// Insert a style element with the unique ID
var styleElementId = 'sr02459pvbvrmhco';
// REPLConsole Constructor
function REPLConsole(config) {
this.commandStorage = new CommandStorage();
this.prompt = config && config.promptLabel ? config.promptLabel : ' >>';
this.commandHandle = config && config.commandHandle ? config.commandHandle : function() { return this; }
}
REPLConsole.prototype.install = function(container) {
var _this = this;
document.onkeydown = function(ev) {
if (_this.focused) { _this.onKeyDown(ev); }
};
document.onkeypress = function(ev) {
if (_this.focused) { _this.onKeyPress(ev); }
};
document.addEventListener('mousedown', function(ev) {
var el = ev.target || ev.srcElement;
if (el) {
do {
if (el === container) {
_this.focus();
return;
}
} while (el = el.parentNode);
_this.blur();
}
});
// Render the console.
container.innerHTML = consoleInnerHtml;
var consoleOuter = findChild(container, 'console-outer');
var consoleActions = findChild(consoleOuter, 'console-actions');
addClass(container, 'console');
addClass(container.getElementsByClassName('layer'), 'pos-absolute border-box');
addClass(container.getElementsByClassName('button'), 'border-box');
addClass(consoleActions, 'pos-fixed pos-right');
// Make the console resizable.
function resizeContainer(ev) {
var startY = ev.clientY;
var startHeight = parseInt(document.defaultView.getComputedStyle(container).height, 10);
var scrollTopStart = consoleOuter.scrollTop;
var clientHeightStart = consoleOuter.clientHeight;
var doDrag = function(e) {
container.style.height = (startHeight + startY - e.clientY) + 'px';
consoleOuter.scrollTop = scrollTopStart + (clientHeightStart - consoleOuter.clientHeight);
shiftConsoleActions();
};
var stopDrag = function(e) {
document.documentElement.removeEventListener('mousemove', doDrag, false);
document.documentElement.removeEventListener('mouseup', stopDrag, false);
};
document.documentElement.addEventListener('mousemove', doDrag, false);
document.documentElement.addEventListener('mouseup', stopDrag, false);
}
function closeContainer(ev) {
container.parentNode.removeChild(container);
}
var shifted = false;
function shiftConsoleActions() {
if (consoleOuter.scrollHeight > consoleOuter.clientHeight) {
var widthDiff = document.documentElement.clientWidth - consoleOuter.clientWidth;
if (shifted || ! widthDiff) return;
shifted = true;
consoleActions.style.marginRight = widthDiff + 'px';
} else if (shifted) {
shifted = false;
consoleActions.style.marginRight = '0px';
}
}
// Initialize
this.outer = consoleOuter;
this.inner = findChild(this.outer, 'console-inner');
this.clipboard = findChild(container, 'clipboard');
this.remotePath = container.dataset.remotePath;
this.newPromptBox();
this.insertCss();
findChild(container, 'resizer').addEventListener('mousedown', resizeContainer);
findChild(consoleActions, 'close-button').addEventListener('click', closeContainer);
consoleOuter.addEventListener('DOMNodeInserted', shiftConsoleActions);
REPLConsole.currentSession = this;
};
// Add CSS styles dynamically. This probably doesnt work for IE <8.
REPLConsole.prototype.insertCss = function() {
if (document.getElementById(styleElementId)) {
return; // already inserted
}
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = consoleStyleCss;
style.id = styleElementId;
document.getElementsByTagName('head')[0].appendChild(style);
};
REPLConsole.prototype.focus = function() {
if (! this.focused) {
this.focused = true;
if (! hasClass(this.inner, "console-focus")) {
addClass(this.inner, "console-focus");
}
this.scrollToBottom();
}
};
REPLConsole.prototype.blur = function() {
this.focused = false;
removeClass(this.inner, "console-focus");
};
/**
* Add a new empty prompt box to the console.
*/
REPLConsole.prototype.newPromptBox = function() {
// Remove the caret from previous prompt display if any.
if (this.promptDisplay) {
this.removeCaretFromPrompt();
}
var promptBox = document.createElement('div');
promptBox.className = "console-prompt-box";
promptBox.innerHTML = promptBoxHtml;
this.promptLabel = promptBox.getElementsByClassName('console-prompt-label')[0];
this.promptDisplay = promptBox.getElementsByClassName('console-prompt-display')[0];
// Render the prompt box
this.setInput("");
this.promptLabel.innerHTML = this.prompt;
this.inner.appendChild(promptBox);
this.scrollToBottom();
};
/**
* Remove the caret from the prompt box,
* mainly before adding a new prompt box.
* For simplicity, just re-render the prompt box
* with caret position -1.
*/
REPLConsole.prototype.removeCaretFromPrompt = function() {
this.setInput(this._input, -1);
};
REPLConsole.prototype.setInput = function(input, caretPos) {
this._caretPos = caretPos === undefined ? input.length : caretPos;
this._input = input;
this.renderInput();
};
/**
* Add some text to the existing input.
*/
REPLConsole.prototype.addToInput = function(val, caretPos) {
caretPos = caretPos || this._caretPos;
var before = this._input.substring(0, caretPos);
var after = this._input.substring(caretPos, this._input.length);
var newInput = before + val + after;
this.setInput(newInput, caretPos + val.length);
};
/**
* Render the input prompt. This is called whenever
* the user input changes, sometimes not very efficient.
*/
REPLConsole.prototype.renderInput = function() {
// Clear the current input.
removeAllChildren(this.promptDisplay);
var promptCursor = document.createElement('span');
promptCursor.className = "console-cursor";
var before, current, after;
if (this._caretPos < 0) {
before = this._input;
current = after = "";
} else if (this._caretPos === this._input.length) {
before = this._input;
current = "\u00A0";
after = "";
} else {
before = this._input.substring(0, this._caretPos);
current = this._input.charAt(this._caretPos);
after = this._input.substring(this._caretPos + 1, this._input.length);
}
this.promptDisplay.appendChild(document.createTextNode(before));
promptCursor.appendChild(document.createTextNode(current));
this.promptDisplay.appendChild(promptCursor);
this.promptDisplay.appendChild(document.createTextNode(after));
};
REPLConsole.prototype.writeOutput = function(output) {
var consoleMessage = document.createElement('pre');
consoleMessage.className = "console-message";
consoleMessage.innerHTML = escapeHTML(output);
this.inner.appendChild(consoleMessage);
this.newPromptBox();
};
REPLConsole.prototype.onEnterKey = function() {
var input = this._input;
if(input != "" && input !== undefined) {
this.commandStorage.addCommand(input);
}
this.commandHandle(input);
};
REPLConsole.prototype.onNavigateHistory = function(offset) {
var command = this.commandStorage.navigate(offset) || "";
this.setInput(command);
};
/**
* Handle control keys like up, down, left, right.
*/
REPLConsole.prototype.onKeyDown = function(ev) {
switch (ev.keyCode) {
case 13:
// Enter key
this.onEnterKey();
ev.preventDefault();
break;
case 80:
// Ctrl-P
if (! ev.ctrlKey) break;
case 38:
// Up arrow
this.onNavigateHistory(-1);
ev.preventDefault();
break;
case 78:
// Ctrl-N
if (! ev.ctrlKey) break;
case 40:
// Down arrow
this.onNavigateHistory(1);
ev.preventDefault();
break;
case 37:
// Left arrow
var caretPos = this._caretPos > 0 ? this._caretPos - 1 : this._caretPos;
this.setInput(this._input, caretPos);
ev.preventDefault();
break;
case 39:
// Right arrow
var length = this._input.length;
var caretPos = this._caretPos < length ? this._caretPos + 1 : this._caretPos;
this.setInput(this._input, caretPos);
ev.preventDefault();
break;
case 8:
// Delete
this.deleteAtCurrent();
ev.preventDefault();
break;
default:
break;
}
if (ev.ctrlKey || ev.metaKey) {
// Set focus to our clipboard in case they hit the "v" key
this.clipboard.focus();
if (ev.keyCode == 86) {
// Pasting to clipboard doesn't happen immediately,
// so we have to wait for a while to get the pasted text.
var _this = this;
setTimeout(function() {
_this.addToInput(_this.clipboard.value);
_this.clipboard.value = "";
_this.clipboard.blur();
}, 10);
}
}
ev.stopPropagation();
};
/**
* Handle input key press.
*/
REPLConsole.prototype.onKeyPress = function(ev) {
// Only write to the console if it's a single key press.
if (ev.ctrlKey || ev.metaKey) { return; }
var keyCode = ev.keyCode || ev.which;
this.insertAtCurrent(String.fromCharCode(keyCode));
ev.stopPropagation();
ev.preventDefault();
};
/**
* Delete a character at the current position.
*/
REPLConsole.prototype.deleteAtCurrent = function() {
if (this._caretPos > 0) {
var caretPos = this._caretPos - 1;
var before = this._input.substring(0, caretPos);
var after = this._input.substring(this._caretPos, this._input.length);
this.setInput(before + after, caretPos);
}
};
/**
* Insert a character at the current position.
*/
REPLConsole.prototype.insertAtCurrent = function(char) {
var before = this._input.substring(0, this._caretPos);
var after = this._input.substring(this._caretPos, this._input.length);
this.setInput(before + char + after, this._caretPos + 1);
};
REPLConsole.prototype.scrollToBottom = function() {
this.outer.scrollTop = this.outer.scrollHeight;
};
// Change the binding of the console
REPLConsole.prototype.switchBindingTo = function(frameId, callback) {
var url = this.remotePath + "/trace";
var params = "frame_id=" + encodeURIComponent(frameId);
postRequest(url, params, callback);
};
/**
* Install the console into the element with a specific ID.
* Example: REPLConsole.installInto("target-id")
*/
REPLConsole.installInto = function(id) {
var consoleElement = document.getElementById(id);
var remotePath = consoleElement.dataset.remotePath;
var replConsole = new REPLConsole({
promptLabel: consoleElement.dataset.initialPrompt,
commandHandle: function(line) {
var _this = this;
var url = remotePath;
var params = "input=" + encodeURIComponent(line);
putRequest(url, params, function(xhr) {
var response = JSON.parse(xhr.responseText);
_this.writeOutput(response.output);
});
}
});
replConsole.install(consoleElement);
return replConsole;
};
// This is to store the latest single session, and the stored session
// is updated by the REPLConsole#install() method.
// It allows to operate the current session from the other scripts.
REPLConsole.currentSession = null;
// DOM helpers
function hasClass(el, className) {
var regex = new RegExp('(?:^|\\s)' + className + '(?!\\S)', 'g');
return el.className && el.className.match(regex);
}
function isNodeList(el) {
return typeof el.length === 'number' &&
typeof el.item === 'function';
}
function addClass(el, className) {
if (isNodeList(el)) {
for (var i = 0; i < el.length; ++ i) {
addClass(el[i], className);
}
} else {
el.className += " " + className;
}
}
function removeClass(el, className) {
var regex = new RegExp('(?:^|\\s)' + className + '(?!\\S)', 'g');
el.className = el.className.replace(regex, '');
}
function removeAllChildren(el) {
while (el.firstChild) {
el.removeChild(el.firstChild);
}
}
function findChild(el, className) {
for (var i = 0; i < el.childNodes.length; ++ i) {
if (hasClass(el.childNodes[i], className)) {
return el.childNodes[i];
}
}
}
function escapeHTML(html) {
return html
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/`/g, '`');
}
// XHR helpers
function request(method, url, params, callback) {
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("Accept", "<%= Mime::WEB_CONSOLE_V2 %>");
xhr.send(params);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
callback(xhr);
}
}
}
function postRequest(url, params, callback) {
request("POST", url, params, callback);
}
function putRequest(url, params, callback) {
request("PUT", url, params, callback);
}
window.REPLConsole = REPLConsole;
|