📄 env.js
字号:
/* * Simulated browser environment for Rhino * Based on the work by by John Resig <http://ejohn.org/> under the MIT License. */// The window objectvar window = this;(function(){ // Browser Navigator window.navigator = { get userAgent() { return 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/2008111318 Ubuntu/8.10 (intrepid) Firefox/3.0.4'; } }; // Setup location properties var _location; window.__defineSetter__("location", function(url) { var urlParts = /(^https?:)\/\/(([^:\/\?#]+)(:(\d+))?)([^\?#]*)?(\?[^#]*)?(#.*)?/.exec(url); _location = { href: url, protocol: urlParts[1], host: urlParts[2], hostname: urlParts[3], port: urlParts[4] ? urlParts[5] : '', pathname: urlParts[6] || '', search: urlParts[7] || '', hash: urlParts[8] || '' }; }); window.__defineGetter__("location", function() { return _location; }); // The output console window.console = function() { function _log(text) { var formatter = new java.text.SimpleDateFormat('yyyy-MM-dd HH:mm:ss.SSS'); var log = formatter.format(new java.util.Date()); log += ' ' + java.lang.Thread.currentThread().getId(); log += ' ' + text; java.lang.System.out.println(log); } return { error: function(text) { _log('ERROR: ' + text); }, warn: function(text) { _log('WARN : ' + text); }, info: function(text) { _log('INFO : ' + text); }, debug: function(text) { _log('DEBUG: ' + text); }, log: function(text) { _log(text); } }; }(); // Timers var _scheduler = new java.util.concurrent.Executors.newSingleThreadScheduledExecutor(); window.setTimeout = function(fn, delay) { return _scheduler.schedule(new java.lang.Runnable({ run: function() { threadModel.execute(window, window, fn); } }), delay, java.util.concurrent.TimeUnit.MILLISECONDS); }; window.clearTimeout = function(handle) { handle.cancel(true); }; window.setInterval = function(fn, period) { return _scheduler.scheduleWithFixedDelay(new java.lang.Runnable({ run: function() { threadModel.execute(window, window, fn); } }), period, period, java.util.concurrent.TimeUnit.MILLISECONDS); }; window.clearInterval = function(handle) { handle.cancel(true); }; // Window Events var events = [{}]; window.addEventListener = function(type, fn) { if (!this.uuid || this == window) { this.uuid = events.length; events[this.uuid] = {}; } if (!events[this.uuid][type]) events[this.uuid][type] = []; if (events[this.uuid][type].indexOf(fn) < 0) events[this.uuid][type].push(fn); }; window.removeEventListener = function(type, fn) { if (!this.uuid || this == window) { this.uuid = events.length; events[this.uuid] = {}; } if (!events[this.uuid][type]) events[this.uuid][type] = []; events[this.uuid][type] = events[this.uuid][type].filter(function(f) { return f != fn; }); }; window.dispatchEvent = function(event) { if (event.type) { if (this.uuid && events[this.uuid][event.type]) { var self = this; events[this.uuid][event.type].forEach(function(fn) { fn.call(self, event); }); } if (this["on" + event.type]) this["on" + event.type].call(self, event); } }; // Helper method for generating the right DOM objects based upon the type var _objNodes = new java.util.HashMap(); function makeNode(node) { if (node) { if (!_objNodes.containsKey(node)) _objNodes.put(node, node.getNodeType() == Packages.org.w3c.dom.Node.ELEMENT_NODE ? new DOMElement(node) : new DOMNode(node)); return _objNodes.get(node); } else return null; } // DOM Document window.DOMDocument = function(stream) { this._file = stream; this._dom = Packages.javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stream); if (!_objNodes.containsKey(this._dom)) _objNodes.put(this._dom, this); }; DOMDocument.prototype = { createTextNode: function(text) { return makeNode(this._dom.createTextNode( text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"))); }, createComment: function(text) { return makeNode(this._dom.createComment(text)); }, createElement: function(name) { return makeNode(this._dom.createElement(name.toLowerCase())); }, getElementsByTagName: function(name) { return new DOMNodeList(this._dom.getElementsByTagName( name.toLowerCase())); }, getElementById: function(id) { var elems = this._dom.getElementsByTagName("*"); for (var i = 0; i < elems.length; i++) { var elem = elems.item(i); if (elem.getAttribute("id") == id) return makeNode(elem); } return null; }, get body() { return this.getElementsByTagName("body")[0]; }, get documentElement() { return makeNode(this._dom.getDocumentElement()); }, get ownerDocument() { return null; }, addEventListener: window.addEventListener, removeEventListener: window.removeEventListener, dispatchEvent: window.dispatchEvent, get nodeName() { return "#document"; }, importNode: function(node, deep) { return makeNode(this._dom.importNode(node._dom, deep)); }, toString: function() { return "Document" + (typeof this._file == "string" ? ": " + this._file : ""); }, get innerHTML() { return this.documentElement.outerHTML; }, get defaultView() { return { getComputedStyle: function(elem) { return { getPropertyValue: function(prop) { prop = prop.replace(/\-(\w)/g, function(m, c) { return c.toUpperCase(); }); var val = elem.style[prop]; if (prop == "opacity" && val == "") val = "1"; return val; } }; } }; }, createEvent: function() { return { type: "", initEvent: function(type) { this.type = type; } }; } }; function getDocument(node) { return _objNodes.get(node); } // DOM NodeList window.DOMNodeList = function(list) { this._dom = list; this.length = list.getLength(); for (var i = 0; i < this.length; i++) { var node = list.item(i); this[i] = makeNode(node); } }; DOMNodeList.prototype = { toString: function() { return "[ " + Array.prototype.join.call(this, ", ") + " ]"; }, get outerHTML() { return Array.prototype.map.call( this, function(node) {return node.outerHTML;}).join(''); } }; // DOM Node window.DOMNode = function(node) { this._dom = node; }; DOMNode.prototype = { get nodeType() { return this._dom.getNodeType(); }, get nodeValue() { return this._dom.getNodeValue(); }, get nodeName() { return this._dom.getNodeName(); }, cloneNode: function(deep) { return makeNode(this._dom.cloneNode(deep)); }, get ownerDocument() { return getDocument(this._dom.ownerDocument); }, get documentElement() { return makeNode(this._dom.documentElement); }, get parentNode() { return makeNode(this._dom.getParentNode()); }, get nextSibling() { return makeNode(this._dom.getNextSibling()); }, get previousSibling() { return makeNode(this._dom.getPreviousSibling()); }, toString: function() { return '"' + this.nodeValue + '"'; }, get outerHTML() { return this.nodeValue; } }; // DOM Element window.DOMElement = function(elem) { this._dom = elem; this.style = { get opacity() { return this._opacity; }, set opacity(val) { this._opacity = val + ""; } }; // Load CSS info var styles = (this.getAttribute("style") || "").split(/\s*;\s*/); for (var i = 0; i < styles.length; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -