joeyoverlay.js.svn-base

来自「Joey is j2me client server application f」· SVN-BASE 代码 · 共 1,413 行 · 第 1/3 页

SVN-BASE
1,413
字号
    	g_joey_gBrowser.selectedBrowser    	               .contentDocument    	               .addEventListener("mousemove"    	                                 ,sigmaCall    	                                 ,false);        omegaCall = function(e) { thisInstance.mouseClickListener(e) };            	g_joey_gBrowser.selectedBrowser    	               .contentDocument    	               .addEventListener("mousedown"    	                                 ,omegaCall     	                                 ,false);            deltaCall = function(e) { thisInstance.keyDownListener(e) };            	g_joey_gBrowser.selectedBrowser    	               .contentDocument    	               .addEventListener("keydown"    	                                 ,deltaCall     	                                 ,false);	    this.runtimer();    // timer-based refresh function..	        },        disable: function () {        if (this.enabled == false)            return;        joeyDumpToConsole("g_joeySelectorService disable");            clearTimeout(this.timer);        this.timer = null;        if (sigmaCall !=null)            g_joey_gBrowser.selectedBrowser         	               .contentDocument    	                   .removeEventListener("mousemove"                                                ,sigmaCall                                                ,false);                if (omegaCall != null)            g_joey_gBrowser.selectedBrowser         	               .contentDocument    	                   .removeEventListener("mousedown"                                                ,omegaCall                                                 ,false);        if (deltaCall != null)            g_joey_gBrowser.selectedBrowser        	               .contentDocument        	               .removeEventListener("keydown"                                                ,deltaCall                                                 ,false);        this.removeBox();        this.associatedDocument    = null;           this.currentEvent          = null;        this.previousTargetElement = null;                   this.enabled = false;    },    mouseMoveListener: function (e) {		if (this.previousTargetElement != e.target) {			this.currentEvent = e;			this.previousTargetElement = e.target;		}                                     },    mouseClickListener: function (e) {        try {        if(e.button == 0) {            /*              * We may revisit this to elect target elements              * if they make sense. For example I assume we dont want to elect              * the hole page. .. or not :)              */             	        joey_selectedTarget(this.currentEvent.target);            e.preventDefault(); // eat the event        }            	} catch (e) {}	    this.disable();    },    keyDownListener: function (e) {        this.disable();        e.preventDefault();    },    createBox: function () {                var newDiv= this.associatedDocument.createElementNS("http://www.w3.org/1999/xhtml", "div");                newDiv.style.position="absolute";                newDiv.style.zIndex="1000";                newDiv.style.background="url(chrome://joey/skin/selector-tile.png)";                newDiv.style.border="0px";                newDiv.style.height="4px";                this.currentElementTop=newDiv;                                   	                var newDiv= this.associatedDocument.createElementNS("http://www.w3.org/1999/xhtml", "div");                newDiv.style.position="absolute";                newDiv.style.zIndex="1000";                newDiv.style.background="url(chrome://joey/skin/selector-tile.png)";                newDiv.style.left="0px";          	                newDiv.style.border="0px";          	                newDiv.style.height="4px";                this.currentElementBottom=newDiv;                this.currentElementTop.appendChild(this.currentElementBottom);    	                var newDiv= this.associatedDocument.createElementNS("http://www.w3.org/1999/xhtml", "div");                newDiv.style.position="absolute";                newDiv.style.zIndex="1000";                newDiv.style.top="0px";                newDiv.style.left="0px";                newDiv.style.width="4px";                newDiv.style.background="url(chrome://joey/skin/selector-tile.png)";                newDiv.style.border="0px";                                this.currentElementLeft=newDiv;                this.currentElementTop.appendChild(this.currentElementLeft);    	                var newDiv = this.associatedDocument.createElementNS("http://www.w3.org/1999/xhtml", "div");                newDiv.style.position="absolute";                newDiv.style.zIndex="1000";                newDiv.style.top="0px";                newDiv.style.width="4px";                newDiv.style.background="url(chrome://joey/skin/selector-tile.png)";                newDiv.style.border="0px";                                this.currentElementRight=newDiv;                this.currentElementTop.appendChild(this.currentElementRight);                                try {                    this.associatedDocument.body.appendChild(this.currentElementTop);                } catch (ignore) {                    joeyDumpToConsole(ignore);                }                                },     removeBox: function () {            try {                    if(this.currentElementTop.parentNode) {                this.currentElementTop.parentNode.removeChild(this.currentElementTop);        	}        	         	        } catch (i) { joeyDumpToConsole(i) }          	        	    },    runtimer: function() {        try {            /*              * We want UI to be smooth so we keep this at 150 miliseconds.              * Otherwise the Contextual Box moves too much in the screen for every little DOM node.             *             */             if (this.currentEvent && this.associatedDocument) {                                var currentDocument = this.associatedDocument;                                var boxObject = currentDocument.getBoxObjectFor(this.currentEvent.target);                                const borderSize=4;                                var boxObjectX = boxObject.x - borderSize;                var boxObjectY = boxObject.y - borderSize;                var rawWidth = boxObject.width;                var rawHeight = boxObject.height;                                var restWidth = rawWidth % 4;                var restHeight = rawHeight % 4;                                var boxCounterWidth = (rawWidth - restWidth)/4 + 1;                 var boxCounterHeight = (rawHeight- restHeight)/4 + 1;                                 var boxObjectWidth  = ( rawWidth - restWidth )  + ( borderSize * 2 );                var boxObjectHeight = ( rawHeight - restHeight )  + ( borderSize * 2 ) ;                                var modOddWidth = boxCounterWidth % 2;                var modOddHeight = boxCounterHeight % 2;                                        if( parseInt(modOddWidth) == 0) {                    boxObjectWidth+=4;                    boxObjectX-=4;                }                            if( parseInt(modOddHeight) == 0) {                    boxObjectHeight+=4;                    boxObjectY-=4;                }                                this.currentElementTop.style.top=boxObjectY+"px";                this.currentElementTop.style.left=boxObjectX+"px";                this.currentElementTop.style.width=boxObjectWidth+"px";                this.currentElementBottom.style.top=boxObjectHeight+"px";                this.currentElementBottom.style.width=boxObjectWidth+4+"px";                this.currentElementLeft.style.height=boxObjectHeight+"px";                this.currentElementRight.style.left=boxObjectWidth+"px";                this.currentElementRight.style.height=boxObjectHeight+4+"px";                            } // end of current event...                        if (this.associatedDocument) {                this.timer = setTimeout("g_joeySelectorService.runtimer()",122);            }	        }        catch(e)        {            // if any of this fails, just kill it.            setTimeout(g_joeySelectorService.disable, 0);        }    } // end of runtimer      }/** * Determine whether a node's text content is entirely whitespace. * * @param nod  A node implementing the |CharacterData| interface (i.e., *             a |Text|, |Comment|, or |CDATASection| node * @return     True if all of the text content of |nod| is whitespace, *             otherwise false. */function is_all_ws( nod ){  // Use ECMA-262 Edition 3 String and RegExp features  return !(/[^\t\n\r ]/.test(nod.data));}/** * Determine if a node should be ignored by the iterator functions. * * @param nod  An object implementing the DOM1 |Node| interface. * @return     true if the node is: *                1) A |Text| node that is all whitespace *                2) A |Comment| node *             and otherwise false. */function is_ignorable( nod ){  return ( nod.nodeType == 8) || // A comment node         ( (nod.nodeType == 3) && is_all_ws(nod) ); // a text node, all ws}/** * Version of |previousSibling| that skips nodes that are entirely * whitespace or comments.  (Normally |previousSibling| is a property * of all DOM nodes that gives the sibling node, the node that is * a child of the same parent, that occurs immediately before the * reference node.) * * @param sib  The reference node. * @return     Either: *               1) The closest previous sibling to |sib| that is not *                  ignorable according to |is_ignorable|, or *               2) null if no such node exists. */function node_before( sib ){  while ((sib = sib.previousSibling)) {    if (!is_ignorable(sib)) return sib;  }  return null;}function joey_buildXPath(targetElement){    if (targetElement == null)        return null;    var buffer = "";    var cur = targetElement;    do {        var name = "";        var sep = "/";        var occur = 0;        var ignore = false;        var type = targetElement.nodeType;         //        alert(buffer);        if (type == Node.DOCUMENT_NODE)        {            buffer = "/" + buffer;            break;        }        else if (type == Node.ATTRIBUTE_NODE)        {            sep = "@";            name = cur.nodeName;            next = cur.parentNode;        }        else        {            if (type == Node.ELEMENT_NODE) {                if (cur.nodeName.toLowerCase() == "a"      || cur.nodeName.toLowerCase() == "img" ||                    cur.nodeName.toLowerCase() == "ul"     || cur.nodeName.toLowerCase() == "document" ||                    cur.nodeName.toLowerCase() == "document" ||                    cur.nodeName.toLowerCase() == "font"   || cur.nodeName.toLowerCase() == "#document" )                    ignore = true;                var id = null;                try {// why would this throw?                    id = cur.getAttribute('id');                } catch (e) {}                if (id != null) {                                        if (buffer == "")                    {                        buffer = "id('"+id+"')";                        return buffer;                    }                    buffer = "id('" + id + "')" + buffer;                    return buffer;                }            }            name = cur.nodeName.toLowerCase();            next = cur.parentNode;            // now figure out the index            var tmp = node_before(cur);            while (tmp != null) {                if (name == tmp.nodeName.toLowerCase()) {                    occur++;                }                tmp = node_before(tmp);            }            occur++;                        if (type != Node.ELEMENT_NODE) {                // fix the names for those nodes where xpath query and dom node name don't match                if (type == Node.COMMENT_NODE) {                    ignore = true;                    name = 'comment()';                }                else if (type == Node.PI_NODE) {                    ignore = true;                    name = 'processing-instruction()';                }                else if (type == Node.TEXT_NODE) {                    ignore = true;                    name = 'text()';                }                // anything left here has not been coded yet (cdata is broken)                else {                    name = '';                    sep = '';                    occur = 0;                }            }        }        if (cur.nodeName.toLowerCase() == "html" ||            cur.nodeName.toLowerCase() == "body" )            occur = 0;        if (ignore == true) {        }        else if (occur == 0) {            buffer = sep + name + buffer;        }        else {            buffer = sep + name + '[' + occur + ']' + buffer;        }        ignore = false;                cur = next;            } while (cur != null);    return buffer;}function toXMLString(str) {    return str.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/\'/g, "&apos;").replace(/\"/g, "&quot;");}function joey_selectedTarget(targetElement){    g_joey_isfile = false;    var focusedWindow = document.commandDispatcher.focusedWindow;    g_joey_url  = focusedWindow.location.href;    g_joey_title = "Microsummary from : " + focusedWindow.location.href;     g_joey_content_type = "microsummary/xml";    var xpath = joey_buildXPath(targetElement);    /*      var xpath = prompt("enter an xpath");    if (!confirm (xpath))        return;    */    var uuidGenerator =  Components.classes["@mozilla.org/uuid-generator;1"].getService(Components.interfaces.nsIUUIDGenerator);    var uuid = uuidGenerator.generateUUID();    var uuidString = uuid.toString();    var str = '<?xml version="1.0" encoding="UTF-8"?>\n'		+ '<generator xmlns="http://www.mozilla.org/microsummaries/0.1" name="Microsummary for '		+ toXMLString(focusedWindow.location.href) + '" '		+ 'uri="urn:' + uuidString + '">\n'		+ ' <pages>\n'		+ '   <include>' 		+ toXMLString(focusedWindow.location.href)		+ '</include>\n';    var hint = toXMLString(targetElement.innerHTML);    hint ="";    str += ' </pages>\n'		+ ' <template>\n'		+ '   <transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">\n'		+ '     <output method="text"/>\n'		+ '     <template match="/">\n'		+ '       <value-of select="' + xpath + '"/>\n'		+ '     </template>\n'		+ '   </transform>\n'		+ ' </template>\n'        + '<hint>' + hint + '</hint>'		+ '</generator>\n';    //    alert(str);    g_joey_data = str;    uploadDataFromGlobals(false);}/*  *  */function joey_enableSelection() {    g_joeySelectorService.enable();}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?