📄 1019856700-common.js
字号:
return lowercase ? uppercase.toLowerCase() : uppercase;}// Case-insensitive string comparatorfunction CompareStringsIgnoreCase(s1, s2) { s1 = s1.toLowerCase(); s2 = s2.toLowerCase(); if (s1 < s2) { return -1; } else if (s1 == s2) { return 0; } else { return 1; }}//------------------------------------------------------------------------// TextArea utilities//------------------------------------------------------------------------// Gets the cursor pos in a text area. Returns -1 if the cursor pos cannot// be determined or if the cursor out of the textfield.function GetCursorPos(win, textfield) { try { if (IsDefined(textfield.selectionEnd)) { // Mozilla directly supports this return textfield.selectionEnd; } else if (win.document.selection && win.document.selection.createRange) { // IE doesn't export an accessor for the endpoints of a selection. // Instead, it uses the TextRange object, which has an extremely obtuse // API. Here's what seems to work: // (1) Obtain a textfield from the current selection (cursor) var tr = win.document.selection.createRange(); // Check if the current selection is in the textfield if (tr.parentElement() != textfield) { return -1; } // (2) Make a text range encompassing the textfield var tr2 = tr.duplicate(); tr2.moveToElementText(textfield); // (3) Move the end of the copy to the beginning of the selection tr2.setEndPoint("EndToStart", tr); // (4) The span of the textrange copy is equivalent to the cursor pos var cursor = tr2.text.length; // Finally, perform a sanity check to make sure the cursor is in the // textfield. IE sometimes screws this up when the window is activated if (cursor > textfield.value.length) { return -1; } return cursor; } else { Debug("Unable to get cursor position for: " + navigator.userAgent); // Just return the size of the textfield // TODO: Investigate how to get cursor pos in Safari! return textfield.value.length; } } catch (e) { DumpException(e, "Cannot get cursor pos"); } return -1;}function SetCursorPos(win, textfield, pos) { if (IsDefined(textfield.selectionEnd) && IsDefined(textfield.selectionStart)) { // Mozilla directly supports this textfield.selectionStart = pos; textfield.selectionEnd = pos; } else if (win.document.selection && textfield.createTextRange) { // IE has textranges. A textfield's textrange encompasses the // entire textfield's text by default var sel = textfield.createTextRange(); sel.collapse(true); sel.move("character", pos); sel.select(); }}//------------------------------------------------------------------------// Array utilities//------------------------------------------------------------------------// Find an item in an array, returns the key, or -1 if not foundfunction FindInArray(array, x) { for (var i = 0; i < array.length; i++) { if (array[i] == x) { return i; } } return -1;}// Inserts an item into an array, if it's not already in the arrayfunction InsertArray(array, x) { if (FindInArray(array, x) == -1) { array[array.length] = x; }}// Delete an element from an arrayfunction DeleteArrayElement(array, x) { var i = 0; while (i < array.length && array[i] != x) i++; array.splice(i, 1);}// Copies a flat arrayfunction CopyArray(array) { var copy = []; for (var i = 0; i < array.length; i++) { copy[i] = array[i]; } return copy;}// Clone an object (recursively)function CloneObject(x) { if ((typeof x) == "object") { var y = []; for (var i in x) { y[i] = CloneObject(x[i]); } return y; } return x;}/** * Clone an event; cannot use CloneObject(event) * because it suffers from infinite recursion. * Thus, only a subset of the event properties are * cloned -- if you need others, just add them * to this function (just don't remove any!) */function CloneEvent(ev) { var clone = {}; clone.clientX = ev.clientX; clone.clientY = ev.clientY; clone.pageX = ev.pageX; clone.pageY = ev.pageY; clone.type = ev.type; clone.srcElement = ev.srcElement; clone.target = ev.target; clone.cancelBubble = ev.cancelBubble; clone.explicitOriginalTarget = ev.explicitOriginalTarget; // add more properties here return clone;}function GetEventTarget(/*Event*/ ev) {// Event is not a type in IE; IE uses Object for events// AssertType(ev, Event, 'arg passed to GetEventTarget not an Event'); return ev.srcElement || ev.target;}/** cancels the event */// from http://www.quirksmode.org/js/events_order.htmlfunction CancelEvent(/*Event*/ ev) { if (is_ie) { ev.cancelBubble = true; } else if (ev.stopPropagation) { ev.stopPropagation(); }}//------------------------------------------------------------------------// Formatting utilities//------------------------------------------------------------------------// A simple printf type function that takes in a template array, and a data// array. e.g. PrintArray(["a",,"b",,"c"], ["x", "y"]) => axbycfunction PrintArray(array, data) { // Check that the argument count is correct. AssertEquals(array.length, data.length * 2 + 1); for (var i = 0, idx = 1; i < data.length; i++, idx += 2) { array[idx] = data[i]; } return array.join("");}function ImageHtml(url, attributes) { return "<img " + attributes + " src=" + url + ">";}// Formats an object id that has two id numbers, eg, foo_3_7function MakeId3(idprefix, m, n) { return idprefix + m + "_" + n;}//------------------------------------------------------------------------// Email address parsing//------------------------------------------------------------------------// Parse an email address of the form "name" <address> into [name, address]function ParseAddress(addr) { var name = ""; var address = ""; for (var i = 0; i < addr.length;) { var token = GetEmailToken(addr, i); if (token.charAt(0) == '<') { var end = token.indexOf(">"); address = token.substring(1, (end != -1) ? end : token.length); } else if (address == "") { name += token; } i += token.length; } // Check if it's a simple email address of the form "jlim@google.com" if (address == "" && name.indexOf("@") != -1) { address = name; name = ""; } name = CollapseWhitespace(name); name = StripQuotes(name, "'"); name = StripQuotes(name, "\""); address = CollapseWhitespace(address); return [name, address];}// Given an email address, get the address partfunction GetAddress(address) { return ParseAddress(address)[1];}// Get the username part of an email addressfunction GetAddressUsername(address) { address = GetAddress(address); var at = address.indexOf("@"); return (at == -1) ? address : address.substr(0, at);}// Given an email address, get the personal partfunction GetPersonal(address) { return ParseAddress(address)[0];}// Given an address, get a short namefunction GetPersonalElseUsername(address) { var personal = GetPersonal(address); if (personal != "") { return personal; } else { return GetAddressUsername(address); }}// Strip ' or " chars around a stringfunction StripQuotes(str, quotechar) { var len = str.length; if (str.charAt(0) == quotechar && str.charAt(len - 1) == quotechar) { return str.substring(1, len - 1); } return str;}// Convert a string containing list of email addresses into an array// of stringsfunction EmailsToArray(str) { var result = []; var email = ""; var token; for (var i = 0; i < str.length; ) { token = GetEmailToken(str, i); if (token == ",") { AddEmailAddress(result, email); email = ""; i++; continue; } email += token; i += token.length; } // Add last if (email !="" || token == ",") { AddEmailAddress(result, email); } return result;}// Get the next token from a position in an address stringvar openers_ = "\"<([";var closers_ = "\">)]";function GetEmailToken(str, pos) { var ch = str.charAt(pos); var p = openers_.indexOf(ch); if (p == -1) return ch; var end_pos = str.indexOf(closers_.charAt(p), pos + 1); var token = (end_pos >= 0) ? str.substring(pos, end_pos + 1) : str.substr(pos); return token;}// Add an email address to the result array.function AddEmailAddress(result, email) { email = CleanEmailAddress(email); result[result.length] = email;}// Clean up email address:// - remove extra spaces// - Surround name with quotes if it contains special characters// to check if we need " quotes// Note: do not use /g in the regular expression, otherwise the// regular expression cannot be reusable.var specialchars_re_ = /[()<>@,;:\\\".\[\]]/;function CleanEmailAddress(str) { var name_address = ParseAddress(str); var name = name_address[0]; var address = name_address[1]; if (name.indexOf("\"") == -1) { // If there's no " var quote_needed = specialchars_re_.test(name); if (quote_needed) { name = "\"" + name + "\""; } } if (name == "") return address; else if (address == "") return name; else return name + " <" + address + ">";}//------------------------------------------------------------------------// Misc//------------------------------------------------------------------------// Compare long hex stringsfunction CompareID(a, b) { if (a.length != b.length) { return (a.length - b.length); } else { return (a < b) ? -1 : (a > b) ? 1 : 0; }}// Check if a value is definedfunction IsDefined(value) { return (typeof value) != 'undefined';}function GetKeyCode(event) { var code; if (event.keyCode) { code = event.keyCode; } else if (event.which) { code = event.which; } return code;}// define a forid function to fetch a DOM node by id.function forid_1(id) { return document.getElementById(id);}function forid_2(id) { return document.all[id];}/** * Fetch an HtmlElement by id. * DEPRECATED: use $ in dom.js */var forid = document.getElementById ? forid_1 : forid_2;function log(msg) { /* a top level window is its own parent. Use != or else fails on IE with * infinite loop. */ try { if (window.parent != window && window.parent.log) { window.parent.log(window.name + '::' + msg); return; } } catch (e) { // Error: uncaught exception: Permission denied to get property Window.log } var logPane = forid('log'); if (logPane) { var logText = '<p class=logentry><span class=logdate>' + new Date() + '</span><span class=logmsg>' + msg + '</span></p>'; logPane.innerHTML = logText + logPane.innerHTML; } else { window.status = msg; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -