home *** CD-ROM | disk | FTP | other *** search
Wrap
Text File | 2004-10-16 | 34.7 KB | 1,311 lines
var objLinkify = { Linkify_Init: function() { this.prefService = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefService).getBranch('linkification.settings.'); this.Linkify_GetPreferences(); this.aProtocol = new Array(); this.aRecognizeProtocolAs = new Array(); var aTextProtocolList = this.sLinkify_TextProtocolList.split(","); var ctr; for (ctr = 0; ctr < aTextProtocolList.length; ++ctr) { var aTextLinkProtocol = aTextProtocolList[ctr].split(":"); if ((aTextLinkProtocol[0].length > 0) && (aTextLinkProtocol[1].length > 0)) { this.aProtocol.push(aTextLinkProtocol[0]); this.aRecognizeProtocolAs.push(aTextLinkProtocol[1]); } } this.aSubdomain = new Array(); this.aRecognizeSubdomainAs = new Array(); var aSubdomainProtocolList = this.sLinkify_SubdomainProtocolList.split(","); for (ctr = 0; ctr < aSubdomainProtocolList.length; ++ctr) { var aTextLinkProtocol = aSubdomainProtocolList[ctr].split(":"); if ((aTextLinkProtocol[0].length > 0) && (aTextLinkProtocol[1].length > 0)) { this.aSubdomain.push(aTextLinkProtocol[0]); this.aRecognizeSubdomainAs.push(aTextLinkProtocol[1]); } } this.aExcludeTags = this.sLinkify_ExcludeTagList.split(","); this.aSpaceTags = this.sLinkify_SpaceTagList.split(","); var sProtocol = "("; for (ctr = 0; ctr < this.aProtocol.length; ++ctr) { sProtocol += "(?:" + this.aProtocol[ctr] + ")|"; } sProtocol = sProtocol.substr(0, sProtocol.length - 1) + ")"; var sSubdomain = "("; for (ctr = 0; ctr < this.aSubdomain.length; ++ctr) { sSubdomain += "(" + this.aSubdomain[ctr] + ")|"; } sSubdomain = sSubdomain.substr(0, sSubdomain.length - 1) + ")"; var sDomainSuffixList = "(museum)|(com\\.[a-z]{2})|(org\\.[a-z]{2})|(net\\.[a-z]{2})|(ac\\.[a-z]{2})|(co\\.[a-z]{2})|(aero)|(coop)|(info)|(name)|(biz)|(com)|(edu)|(gov)|(int)|(mil)|(net)|(org)|(pro)|(cc)"; var sAllDomainSuffix = "(" + sDomainSuffixList + "|([a-z]{2}))"; var sRegDomainSuffix = "(" + sDomainSuffixList + ")"; var sIPAddress = "((((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([1-9][0-9])|[0-9])\\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([1-9][0-9])|[0-9]))"; var sDomain = "(\\w+(\\-\\w+)*(\\.\\w+(\\-\\w+)*)*)"; var sDomainOther = "((\\w+(\\-\\w+)*)(\\.\\w+(\\-\\w+)*)+)"; var sUserAuth = "(([\\w\\.\\%][\\-\\@]?)+(\\:([\\w\\.\\%]\\-?)+)?\\@)"; var sPortAndPathOptional = "((:\\d+)?((([\\/\\#\\?]+((\\&(amp;)?)|[\\w\\~\\%\\=\\-\\+\\,\\.\\:\\/\\|\\$\\;])+)+[\\&\\w\\/\\#\\%\\=\\+\\:\\/\\|\\$])|([\\/\\#\\?\\$]+)|(\\b)))"; var sPortAndPathRequired = "((:\\d+)?((([\\/\\#\\?]+((\\&(amp;)?)|[\\w\\~\\%\\=\\-\\+\\,\\.\\:\\/\\|\\$\\;])+)+[\\&\\w\\/\\#\\%\\=\\+\\:\\/\\|\\$])|([\\/\\#\\?\\$]+)))"; var sRegExpHTTP = sProtocol + "(\\:\\/\\/" + sUserAuth + "?(((localhost)|" + sIPAddress + "|(" + sDomain + "\\." + sAllDomainSuffix + "))" + sPortAndPathOptional + "))"; var sRegExpWWW = sUserAuth + "?(" + sSubdomain + "\\." + sDomain + "\\." + sAllDomainSuffix + ")" + sPortAndPathOptional; var sRegExpOther = "((" + sUserAuth + sDomainOther + "\\." + sRegDomainSuffix + sPortAndPathOptional + ")|(" + sDomainOther + "\\." + sRegDomainSuffix + sPortAndPathRequired + "))"; var sRegExpIP = "((" + sUserAuth + sIPAddress + sPortAndPathOptional + ")|(" + sIPAddress + sPortAndPathRequired + "))"; var sRegExpEmail = "((([\\w\\.\\-\\+])+\\@)((" + sIPAddress + ")|(" + sDomainOther + ")))"; this.aRegExpListConfined = new Array(); this.aRegExpListConfined.push(new RegExp("^" + sRegExpHTTP + "$", "i")); this.aRegExpListConfined.push(new RegExp("^" + sRegExpWWW + "$", "i")); this.aRegExpListConfined.push(new RegExp("^" + sRegExpOther + "$", "i")); this.aRegExpListConfined.push(new RegExp("^" + sRegExpIP + "$", "i")); this.aRegExpListConfined.push(new RegExp("^" + sRegExpEmail + "$", "i")); this.aRegExpList = new Array(); this.aRegExpList.push(new RegExp(sRegExpHTTP, "i")); this.aRegExpList.push(new RegExp(sRegExpWWW, "i")); this.aRegExpList.push(new RegExp(sRegExpOther, "i")); this.aRegExpList.push(new RegExp(sRegExpIP, "i")); this.aRegExpList.push(new RegExp(sRegExpEmail, "i")); this.aRegExpDesc = new Array(); this.aRegExpDesc.push("HTTP"); this.aRegExpDesc.push("WWW"); this.aRegExpDesc.push("Other"); this.aRegExpDesc.push("StandaloneIP"); this.aRegExpDesc.push("Email"); this.aRegExpTarget = new Array(); this.aRegExpTarget.push(""); this.aRegExpTarget.push(""); this.aRegExpTarget.push("http://$1"); this.aRegExpTarget.push("http://$1"); this.aRegExpTarget.push("mailto:$1"); var sRegExpAll = "("; for (ctr = 0; ctr < this.aRegExpList.length; ++ctr) { sRegExpAll += this.aRegExpList[ctr].source + "|"; } sRegExpAll = sRegExpAll.substr(0, sRegExpAll.length - 1) + ")"; this.rRegExpAll = new RegExp(sRegExpAll, "i"); this.nStockRegExps = this.aRegExpList.length; this.bAllLinkified = false; // End Init Globals }, Linkify_Process: function(ndTarget) { this.Linkify_GetPreferences(); this.Linkify_GetBody(ndTarget); if (!this.bLinkify_Toggle && !this.bFromPopup) { return false; } if (!this.bFromPopup) { var aSiteList = this.sLinkify_SiteList.split(","); var sLocation = window._content.document.location.href; sLocation = sLocation.substr(sLocation.indexOf("://") + 3); sLocation = sLocation.substr(0, sLocation.indexOf("/")); var ctr; for (ctr = 0; ctr < aSiteList.length; ++ctr) { if (aSiteList[ctr].length > sLocation.length) { continue; } if (sLocation.substr(sLocation.length - aSiteList[ctr].length).toLowerCase() == aSiteList[ctr].toLowerCase()) { return false; } } } this.bAllLinkified = true; if (this.ndBody) { if (!this.bFromPopup) { if (this.ndDocument.getElementsByTagName("applet").length > 0) { this.bAllLinkified = false; return false; } } this.nCharNum = 0; this.nCharNumNoSpaces = 0; this.aSpaceBeforeIndex = new Array(); this.aPath = new Array(); this.aPathStart = new Array(); this.aPathLength = new Array(); this.aText = new Array(); this.aTextStart = new Array(); this.aLinkType = new Array(); if (!this.bFromPopup && this.bLinkify_CharLimitEnabled) { var objRange = document.createRange(); objRange.setEnd(this.ndBody, this.ndBody.childNodes.length); objRange.setStart(this.ndBody, 0); if (objRange.toString().length > this.nLinkify_CharLimit) { this.bAllLinkified = false; return false; } } this.sText = this.Linkify_GetText(this.ndBody); this.Linkify_GetAllLinks(); this.Linkify_ReplaceText(); } return false; }, Linkify_GetBody: function(ndTarget) { this.ndDocument = ndTarget; this.ndBody = this.ndDocument.getElementsByTagName("body"); if (this.ndBody) { this.ndBody = this.ndBody[0]; } }, Linkify_InArray: function(sSearch, aArray) { for (var ctr = 0; ctr < aArray.length; ++ctr) { var rMatch = new RegExp("^(" + aArray[ctr] + ")$", "i"); if (rMatch.test(sSearch)) { return ctr; } } return -1; }, Linkify_GetText: function(ndParent) { this.aSpaceBeforeIndex = new Array(); var sText = ""; var sPath = ""; if (arguments.length == 2) { sPath = arguments[1]; } var ndChildren = ndParent.childNodes; var nNumChildren = ndChildren.length; var sCurrentPath; for (var ctr = 0; ctr < nNumChildren; ++ctr) { sCurrentPath = sPath + "" + ctr + "-"; var sNodeName = ndChildren[ctr].nodeName; if (sNodeName == "#text") { var sNodeText = ndChildren[ctr].nodeValue; this.aPathStart[this.aPathStart.length] = this.nCharNum; this.aPathLength[this.aPathLength.length] = sNodeText.length; this.aPath[this.aPath.length] = sCurrentPath.substr(0, sCurrentPath.length - 1); sText += sNodeText; this.nCharNum += sNodeText.length; } else { if (this.Linkify_InArray(sNodeName, this.aExcludeTags) > -1) { var sNodeText = " "; this.aPathStart[this.aPathStart.length] = this.nCharNum; this.aPathLength[this.aPathLength.length] = sNodeText.length; this.aPath[this.aPath.length] = sCurrentPath.substr(0, sCurrentPath.length - 1); sText += sNodeText; this.nCharNum += sNodeText.length; continue; } if (this.Linkify_InArray(sNodeName, this.aSpaceTags) > -1) { var sNodeText = " "; this.aPathStart[this.aPathStart.length] = this.nCharNum; this.aPathLength[this.aPathLength.length] = sNodeText.length; this.aPath[this.aPath.length] = sCurrentPath.substr(0, sCurrentPath.length - 1); sText += sNodeText; this.nCharNum += sNodeText.length; sText += this.Linkify_GetText(ndChildren[ctr], sCurrentPath) + " "; this.nCharNum += 1; continue; } else { sText += this.Linkify_GetText(ndChildren[ctr], sCurrentPath); } } } return sText; }, Linkify_GetAllLinks: function() { var aMatch, sMatch; var bAddLink, sImageCheck, sFile, nLocation; var nTempStart = 0; var sTempText = this.sText; while (aMatch = sTempText.match(this.rRegExpAll)) { sMatch = aMatch[0]; bAddLink = true; nLinkType = this.Linkify_GetLinkType(sMatch, false); if (nLinkType == -1) { bAddLink = false; } if (!this.bLinkify_LinkifyImages) { sFile = sMatch; if (sFile.indexOf("?") > -1) { sFile = sFile.substr(0, sFile.indexOf("?")); } if (sFile.indexOf("#") > -1) { sFile = sFile.substr(0, sFile.indexOf("#")); } sImageCheck = sFile.substr(sFile.length - 5).toLowerCase(); if (sImageCheck == ".jpeg") { bAddLink = false; } sImageCheck = sImageCheck.substr(1); if ((sImageCheck == ".jpg") || (sImageCheck == ".gif") || (sImageCheck == ".png") || (sImageCheck == ".bmp")) { bAddLink = false; } } nLocation = sTempText.indexOf(sMatch); if (bAddLink) { this.aText.push(sMatch); this.aTextStart.push(nTempStart + nLocation); this.aLinkType.push(nLinkType); } nTempStart += (nLocation + sMatch.length); sTempText = sTempText.substr((nLocation + sMatch.length), (sTempText.length - nLocation + sMatch.length)); } return true; }, Linkify_GetLinkType: function(sText, bDoubleClick) { var nLinkType = -1; var ctr; for (ctr = this.aRegExpList.length - 1; ctr >= 0; --ctr) { if (this.aRegExpListConfined[ctr].test(sText)) { nLinkType = ctr; if (this.aRegExpDesc[ctr] == "Custom") { if (!this.bLinkify_Custom && !bDoubleClick) { nLinkType = -1; } return nLinkType; } if (this.aRegExpDesc[ctr] == "Email") { if (!this.bLinkify_Email && !bDoubleClick) { nLinkType = -1; } return nLinkType; } } } if ((this.aRegExpDesc[nLinkType] == "HTTP") && !this.bLinkify_HTTP && !bDoubleClick) { nLinkType = -1; } else if ((this.aRegExpDesc[nLinkType] == "WWW") && !this.bLinkify_WWW && !bDoubleClick) { nLinkType = -1; } else if ((this.aRegExpDesc[nLinkType] == "StandaloneIP") && !this.bLinkify_IP && !bDoubleClick) { nLinkType = -1; } else if ((this.aRegExpDesc[nLinkType] == "Other") && !this.bLinkify_Other && !bDoubleClick) { nLinkType = -1; } return nLinkType; }, Linkify_GetCharIndex: function(nChar) { for (var ctr = (this.aPathStart.length - 1); ctr >= 0; --ctr) { if (this.aPathStart[ctr] <= nChar) { return ctr; } } return -1; }, Linkify_TraversePath: function(aNodePath) { var ndNode = this.ndBody; for (var ctr = 0; ctr < aNodePath.length; ++ctr) { var nNode = parseInt(aNodePath[ctr], 10); ndNode = ndNode.childNodes[nNode]; } return ndNode; }, Linkify_FindCharacter: function(ndNode, nCharNum) { var nStart = 0; var nEnd = ndNode.childNodes.length; var nMid; if (arguments.length == 4) { nStart = arguments[2]; nEnd = arguments[3]; } if (ndNode.nodeName == "#text") { aReturn = new Array(); aReturn.push(ndNode); aReturn.push(nCharNum); return aReturn; } while ((nEnd - nStart) == 1) { ndNode = ndNode.childNodes[nStart]; if (ndNode.nodeName == "#text") { aReturn = new Array(); aReturn.push(ndNode); aReturn.push(nCharNum); return aReturn; } nStart = 0; nEnd = ndNode.childNodes.length; } nMid = Math.ceil((nStart + nEnd) / 2); var objRange = document.createRange(); objRange.setEnd(ndNode, nMid); objRange.setStart(ndNode, nStart); var sFirstHalf = objRange.toString(); if (sFirstHalf.length > nCharNum) { return this.Linkify_FindCharacter(ndNode, nCharNum, nStart, nMid); } nCharNum -= sFirstHalf.length; objRange.setEnd(ndNode, nEnd); objRange.setStart(ndNode, nMid); return this.Linkify_FindCharacter(ndNode, nCharNum, nMid, nEnd); }, Linkify_IsTextNodeWithinExcludeTag: function(ndNode) { while (ndNode.nodeName.toLowerCase() != "body") { if (this.Linkify_InArray(ndNode.parentNode.nodeName, this.aExcludeTags) > -1) { return true; } ndNode = ndNode.parentNode; } return false; }, Linkify_IsTextNodeWithinMouseEventTag: function(ndNode) { if (this.bLinkify_OnClick) { return false; } while (ndNode.nodeName.toLowerCase() != "#document") { if (ndNode.getAttribute("onclick") || ndNode.parentNode.getAttribute("onmousedown")) { return true; } if (ndNode.nodeName.toLowerCase() == "body") { break; } ndNode = ndNode.parentNode; } return false; }, Linkify_IsTextNodeWithinSpaceTag: function(ndNode) { var aSideIsSpace = new Array(); aSideIsSpace.push(false); aSideIsSpace.push(false); var sPosition = ""; if (ndNode.nodeName.toLowerCase() != "body") { if ((ndNode.previousSibling) && (this.Linkify_InArray(ndNode.previousSibling.nodeName, this.aSpaceTags) > -1)) { aSideIsSpace[0] = true; } if ((ndNode.nextSibling) && (this.Linkify_InArray(ndNode.nextSibling.nodeName, this.aSpaceTags) > -1)) { aSideIsSpace[1] = true; } } while (ndNode.nodeName.toLowerCase() != "body") { if (aSideIsSpace[0] && aSideIsSpace[1]) { return aSideIsSpace; } var ndFirst = ndNode.parentNode.firstChild; var ndLast = ndNode.parentNode.lastChild; if (ndFirst != ndLast) { if (ndFirst == ndNode) { if (sPosition == "Last") { return aSideIsSpace; } sPosition = "First" } else if (ndLast == ndNode) { if (sPosition == "First") { return aSideIsSpace; } sPosition = "Last" } else { if ((sPosition == "First") && (ndNode.previousSibling) && (this.Linkify_InArray(ndNode.previousSibling.nodeName, this.aSpaceTags) > -1)) { aSideIsSpace[0] = true; } if ((sPosition == "Last") && (ndNode.nextSibling) && (this.Linkify_InArray(ndNode.nextSibling.nodeName, this.aSpaceTags) > -1)) { aSideIsSpace[1] = true; } return aSideIsSpace; } } var nSpaceIndex = this.Linkify_InArray(ndNode.parentNode.nodeName, this.aSpaceTags); if (nSpaceIndex > -1) { if ((sPosition == "First") || (sPosition == "")) { aSideIsSpace[0] = true; } if ((sPosition == "Last") || (sPosition == "")) { aSideIsSpace[1] = true; } } ndNode = ndNode.parentNode; } return aSideIsSpace; }, Linkify_GroupTextNodes: function(ndNode) { if (ndNode.nodeName != "#text") { return false; } var nCharNum = -1; var nPreLimit = -1; var nPostLimit = -1; if (arguments.length == 4) { nCharNum = arguments[1]; nPreLimit = arguments[2]; nPostLimit = arguments[3]; } var aTextNodes = new Array(); aTextNodes.push(ndNode); var objRange = document.createRange(); objRange.setEnd(ndNode, 0); objRange.setStart(this.ndBody, 0); var nPreLength = nCharNum; while (objRange.toString().length > 0) { var ndPreviousNode = this.Linkify_FindCharacter(this.ndBody, objRange.toString().length - 1)[0]; if (this.Linkify_IsTextNodeWithinSpaceTag(ndPreviousNode)[1]) { break; } if (this.Linkify_IsTextNodeWithinExcludeTag(ndPreviousNode)) { break; } nPreLength += ndPreviousNode.nodeValue.length; aTextNodes.splice(0, 0, ndPreviousNode); objRange.setEnd(ndPreviousNode, 0); if ((nPreLength > -1) && (nPreLimit > -1) && (nPreLength > nPreLimit)) { break; } } objRange.setEnd(ndNode, ndNode.nodeValue.length); var ndNextNode = ndNode; var nPostLength = ndNode.nodeValue.length - nCharNum; while (objRange.toString().length > 0) { var ndTempNode = this.Linkify_FindCharacter(this.ndBody, objRange.toString().length + 1)[0]; if (ndTempNode == ndNextNode) { break; } var ndNextNode = ndTempNode; var aNodeSpaces = this.Linkify_IsTextNodeWithinSpaceTag(ndNextNode); var bNodeExcluded = this.Linkify_IsTextNodeWithinExcludeTag(ndNextNode) if (aNodeSpaces[0] || bNodeExcluded) { break; } if (aNodeSpaces[1]) { aTextNodes.push(ndNextNode); break; } nPostLength += ndNextNode.nodeValue.length; aTextNodes.push(ndNextNode); objRange.setEnd(ndNextNode, ndNextNode.nodeValue.length); if ((nPostLength > -1) && (nPostLimit > -1) && (nPostLength > nPostLimit)) { break; } } return aTextNodes; }, Linkify_ReplaceText: function() { for (var ctr = (this.aTextStart.length - 1); ctr >= 0; --ctr) { var nLinkType = this.aLinkType[ctr]; var sText = this.aText[ctr]; var nTextStart = this.aTextStart[ctr]; var nTextEnd = nTextStart + sText.length; var nIndex = this.Linkify_GetCharIndex(nTextStart); var nEndIndex = this.Linkify_GetCharIndex(nTextEnd - 1); var aIndexPath = this.aPath[nIndex].split("-"); var aEndIndexPath = this.aPath[nEndIndex].split("-"); var bTextBeginsNode = false; var bTextEndsNode = false; if (this.aPathStart[nIndex] == nTextStart) { bTextBeginsNode = true; while (aIndexPath.length > aEndIndexPath.length) { aIndexPath.splice(aIndexPath.length - 1, 1); } } if ((this.aPathStart[nEndIndex] + this.aPathLength[nEndIndex]) == (nTextStart + sText.length)) { bTextEndsNode = true; while (aEndIndexPath.length > aIndexPath.length) { aEndIndexPath.splice(aEndIndexPath.length - 1, 1); } } if (aIndexPath.length != aEndIndexPath.length) { continue; } while (bTextBeginsNode && bTextEndsNode && (aIndexPath.length > 1) && (aIndexPath[aIndexPath.length - 2] != aEndIndexPath[aEndIndexPath.length - 2])) { aIndexPath.splice(aIndexPath.length - 1, 1); aEndIndexPath.splice(aEndIndexPath.length - 1, 1); } var ndNode = this.Linkify_TraversePath(aIndexPath); var ndParent = ndNode.parentNode; if (this.Linkify_IsTextNodeWithinMouseEventTag(ndParent)) { continue; } var ndEndNode = this.Linkify_TraversePath(aEndIndexPath); if (!bTextEndsNode) { ndEndNode.splitText(nTextEnd - this.aPathStart[nEndIndex]); } if (!bTextBeginsNode) { ndNode.splitText(nTextStart - this.aPathStart[nIndex]); ndNode = ndNode.nextSibling; if (this.aPath[nIndex] == this.aPath[nEndIndex]) { ndEndNode = ndEndNode.nextSibling; } } var ndNewNode = this.ndDocument.createElement("a"); var sLinkLocation = this.Linkify_GetLinkLocation(nLinkType, sText); ndNewNode.setAttribute("href", sLinkLocation); ndNewNode.setAttribute("class", "linkification-ext"); var sStyle = ""; if (this.bLinkify_HighlightText) { sStyle += "color:" + this.sLinkify_TextColor; } if (this.bLinkify_HighlightBG) { if (sStyle.length > 0) { sStyle += "; "; } sStyle += "background-color:" + this.sLinkify_BackgroundColor; } if (sStyle.length > 0) { ndNewNode.setAttribute("style", sStyle); } while (ndNode != ndEndNode) { var ndClone = ndNode.cloneNode(true); ndNewNode.appendChild(ndClone); ndNode = ndNode.nextSibling; ndParent.removeChild(ndNode.previousSibling); } var ndClone = ndEndNode.cloneNode(true); ndNewNode.appendChild(ndClone); ndParent.replaceChild(ndNewNode, ndEndNode); } }, Linkify_GetLinkLocation: function(nLinkType, sText) { if (this.aRegExpDesc[nLinkType] == "HTTP") { sText.match(this.aRegExpList[nLinkType]); var sProtocol = RegExp.$1; var sAddress = RegExp.$2; sProtocol = sProtocol.toLowerCase(); var nUseProtocol = this.Linkify_InArray(sProtocol, this.aProtocol); var sUseProtocol = ""; if (nUseProtocol > -1) { sUseProtocol = this.aRecognizeProtocolAs[nUseProtocol]; } if (sUseProtocol == "") { return sText; } else { return sUseProtocol + sAddress; } } else if (this.aRegExpDesc[nLinkType] == "WWW") { sText.match(this.aRegExpList[nLinkType]); var sSubdomain = RegExp.$6; sSubdomain = sSubdomain.toLowerCase(); var nUseProtocol = this.Linkify_InArray(sSubdomain, this.aSubdomain); var sUseProtocol = "http"; if (nUseProtocol > -1) { sUseProtocol = this.aRecognizeSubdomainAs[nUseProtocol]; } return sUseProtocol + "://" + sText; } else { return sText.replace(this.aRegExpList[nLinkType], this.aRegExpTarget[nLinkType]); } return ""; }, Linkify_Undo: function() { if (!this.bAllLinkified) { return false; } this.bAllLinkified = false; var aAnchors = window._content.document.getElementsByTagName("a"); for (var ctr = aAnchors.length - 1; ctr >= 0; --ctr) { if (aAnchors[ctr].getAttribute("class") == "linkification-ext") { while (aAnchors[ctr].childNodes.length > 0) { aAnchors[ctr].parentNode.insertBefore(aAnchors[ctr].removeChild(aAnchors[ctr].firstChild), aAnchors[ctr]); } aAnchors[ctr].parentNode.removeChild(aAnchors[ctr]); } } }, Linkify_GetPreferences: function() { this.bLinkify_HighlightText = false; this.bLinkify_HighlightBG = false; this.sLinkify_TextColor = "#006620"; this.sLinkify_BackgroundColor = "#FFF9AB"; this.bLinkify_Toggle = true; this.bLinkify_DoubleClick = true; this.bLinkify_OnClick = true; this.bLinkify_Popup = false; this.bLinkify_OpenInTab = false; this.bLinkify_OpenTabInBG = false; this.bLinkify_SuppressReferer = true; this.bLinkify_LinkifyImages = true; this.bLinkify_HTTP = true; this.bLinkify_Email = true; this.bLinkify_WWW = true; this.bLinkify_IP = true; this.bLinkify_Other = true; this.sLinkify_TextProtocolList = "h..p:http,h...s:https,f.p:ftp,news:news,nntp:nntp,telnet:telnet"; this.sLinkify_SubdomainProtocolList = "www:http,ftp:ftp"; this.sLinkify_SiteList = "localhost,.google.com"; this.sLinkify_ExcludeTagList = "a,script,select,option,textarea"; this.sLinkify_SpaceTagList = "br,table,tbody,tr,td,p,div,input,ul,ol,li"; this.bLinkify_CharLimitEnabled = true; this.nLinkify_CharLimit = 15000; var sCustomRegExp = ""; var sCustomRegExpDesc = ""; var sCustomRegExpTarget = ""; var bCustomCase = false; if (this.prefService.prefHasUserValue('Linkify_HighlightText')) { this.bLinkify_HighlightText = this.prefService.getBoolPref('Linkify_HighlightText'); } if (this.prefService.prefHasUserValue('Linkify_TextColor')) { this.sLinkify_TextColor = this.prefService.getCharPref('Linkify_TextColor'); } if (this.prefService.prefHasUserValue('Linkify_HighlightBG')) { this.bLinkify_HighlightBG = this.prefService.getBoolPref('Linkify_HighlightBG'); } if (this.prefService.prefHasUserValue('Linkify_BackgroundColor')) { this.sLinkify_BackgroundColor = this.prefService.getCharPref('Linkify_BackgroundColor'); } if (this.prefService.prefHasUserValue('Linkify_Toggle')) { this.bLinkify_Toggle = this.prefService.getBoolPref('Linkify_Toggle'); } if (this.prefService.prefHasUserValue('Linkify_DoubleClick')) { this.bLinkify_DoubleClick = this.prefService.getBoolPref('Linkify_DoubleClick'); } if (this.prefService.prefHasUserValue('Linkify_OnClick')) { this.bLinkify_OnClick = this.prefService.getBoolPref('Linkify_OnClick'); } if (this.prefService.prefHasUserValue('Linkify_Popup_Toggle')) { this.bLinkify_Popup = this.prefService.getBoolPref('Linkify_Popup_Toggle'); } if (this.prefService.prefHasUserValue('Linkify_Custom')) { this.bLinkify_Custom = this.prefService.getCharPref('Linkify_Custom'); } if (this.prefService.prefHasUserValue('Linkify_CustomCase')) { bCustomCase = this.prefService.getBoolPref('Linkify_CustomCase'); } if (this.prefService.prefHasUserValue('Linkify_CustomRegExp')) { sCustomRegExp = this.prefService.getCharPref('Linkify_CustomRegExp'); } if (this.prefService.prefHasUserValue('Linkify_CustomTarget')) { sCustomRegExpTarget = this.prefService.getCharPref('Linkify_CustomTarget'); } if (this.prefService.prefHasUserValue('Linkify_OpenInTab')) { this.bLinkify_OpenInTab = this.prefService.getBoolPref('Linkify_OpenInTab'); } if (this.prefService.prefHasUserValue('Linkify_OpenTabInBG')) { this.bLinkify_OpenTabInBG = this.prefService.getBoolPref('Linkify_OpenTabInBG'); } if (this.prefService.prefHasUserValue('Linkify_SuppressReferer')) { this.bLinkify_SuppressReferer = this.prefService.getBoolPref('Linkify_SuppressReferer'); } if (this.prefService.prefHasUserValue('Linkify_LinkifyImages')) { this.bLinkify_LinkifyImages = this.prefService.getBoolPref('Linkify_LinkifyImages'); } if (this.prefService.prefHasUserValue('Linkify_HTTP')) { this.bLinkify_HTTP = this.prefService.getBoolPref('Linkify_HTTP'); } if (this.prefService.prefHasUserValue('Linkify_Email')) { this.bLinkify_Email = this.prefService.getBoolPref('Linkify_Email'); } if (this.prefService.prefHasUserValue('Linkify_WWW')) { this.bLinkify_WWW = this.prefService.getBoolPref('Linkify_WWW'); } if (this.prefService.prefHasUserValue('Linkify_IP')) { this.bLinkify_IP = this.prefService.getBoolPref('Linkify_IP'); } if (this.prefService.prefHasUserValue('Linkify_Other')) { this.bLinkify_Other = this.prefService.getBoolPref('Linkify_Other'); } if (this.prefService.prefHasUserValue('Linkify_TextProtocolList')) { this.sLinkify_TextProtocolList = this.prefService.getCharPref('Linkify_TextProtocolList'); } if (this.prefService.prefHasUserValue('Linkify_SubdomainProtocolList')) { this.sLinkify_SubdomainProtocolList = this.prefService.getCharPref('Linkify_SubdomainProtocolList'); } if (this.prefService.prefHasUserValue('Linkify_SiteList')) { this.sLinkify_SiteList = this.prefService.getCharPref('Linkify_SiteList'); } if (this.prefService.prefHasUserValue('Linkify_ExcludeTagList')) { this.sLinkify_ExcludeTagList = this.prefService.getCharPref('Linkify_ExcludeTagList'); } if (this.prefService.prefHasUserValue('Linkify_SpaceTagList')) { this.sLinkify_SpaceTagList = this.prefService.getCharPref('Linkify_SpaceTagList'); } if (this.prefService.prefHasUserValue('Linkify_CharLimitEnabled')) { this.bLinkify_CharLimitEnabled = this.prefService.getBoolPref('Linkify_CharLimitEnabled'); } if (this.prefService.prefHasUserValue('Linkify_CharLimit')) { this.nLinkify_CharLimit = this.prefService.getIntPref('Linkify_CharLimit'); } if (sCustomRegExp.length > 0) { var sModifiers = ""; if (!bCustomCase) { sModifiers += "i"; } this.aRegExpList[this.aRegExpList.length] = new RegExp(sCustomRegExp, sModifiers); this.aRegExpDesc[this.aRegExpDesc.length] = "Custom"; this.aRegExpTarget[this.aRegExpTarget.length] = sCustomRegExpTarget; } }, Linkify_ToggleSuppressReferer: function() { this.prefService.setBoolPref('Linkify_SuppressReferer', !this.bLinkify_SuppressReferer); this.bLinkify_SuppressReferer = !this.bLinkify_SuppressReferer; }, Linkify_ToggleLinkify: function() { this.prefService.setBoolPref('Linkify_Toggle', !this.bLinkify_Toggle); this.bLinkify_Toggle = !this.bLinkify_Toggle; }, Linkify_FromPopup: function() { if (this.bFromPopup) { return false; } this.bFromPopup = true; if (this.bAllLinkified) { this.Linkify_Undo(); this.bAllLinkified = false; } else { this.Linkify_Process(window._content.document); this.bAllLinkified = true; } this.bFromPopup = false; }, Linkify_ClickLink: function(sHREF, nButton, bFromAnchor) { if (bFromAnchor && !this.bLinkify_SuppressReferer && !this.bLinkify_OpenInTab) { return false; } var objReferer = getReferrer(window.document); if (this.bLinkify_SuppressReferer) { objReferer = null; } var ndBrowser = window.getBrowser(); if (nButton == 1) { return true; } var sProtocol = sHREF.substr(0, sHREF.indexOf(":")).toLowerCase(); if (((sProtocol != "http") && (sProtocol != "https") && (sProtocol != "ftp")) || (!this.bLinkify_OpenInTab)) { ndBrowser.loadURI(sHREF, objReferer, null); } else { var objTab = ndBrowser.addTab(sHREF, objReferer, null); if (!this.bLinkify_OpenTabInBG) { ndBrowser.selectedTab = objTab; } } return true; }, Linkify_DoubleClickLink: function(objSelection) { if (!this.bLinkify_DoubleClick) { return false; } this.Linkify_GetBody(window._content.document); var objRange = document.createRange(); objRange.setEnd(objSelection.anchorNode, objSelection.anchorOffset); objRange.setStart(this.ndBody, 0); var sPreText = objRange.toString(); if (sPreText.match(/\s[^\s]*$/i)) { var nWhiteSpace = sPreText.search(/\s[^\s]*$/i) + 1; if (nWhiteSpace == sPreText.length) { sPreText = ""; } else { sPreText = sPreText.substr(nWhiteSpace); } } objRange.setEnd(this.ndBody, this.ndBody.childNodes.length); objRange.setStart(objSelection.anchorNode, objSelection.anchorOffset + 1); var sPostText = objRange.toString(); if (sPostText.match(/\s/i)) { var nWhiteSpace = sPostText.search(/\s/i); sPostText = sPostText.substr(0, nWhiteSpace); } var nPreLimit = sPreText.length; var nPostLimit = sPostText.length; var aGroupText = this.Linkify_GroupTextNodes(objSelection.anchorNode, objSelection.anchorOffset, nPreLimit, nPostLimit); if (!aGroupText) { return false; } var sText = ""; for (var ctr = 0; ctr < aGroupText.length; ++ctr) { sText += aGroupText[ctr].nodeValue; } objRange.setStart(this.ndBody, 0); objRange.setEnd(aGroupText[0], 0); var nPreChars = objRange.toString().length; objRange.setEnd(objSelection.anchorNode, objSelection.anchorOffset); var nAnchorOffset = objRange.toString().length - nPreChars; var nTempLocation = 0; while (aMatch = sText.match(this.rRegExpAll)) { var sMatch = aMatch[0]; nTempLocation += sText.indexOf(sMatch); if ((nAnchorOffset < nTempLocation) || (nAnchorOffset > (nTempLocation + sMatch.length))) { sText = sText.substr(sText.indexOf(sMatch) + sMatch.length); nTempLocation += sMatch.length; continue; } nLinkType = this.Linkify_GetLinkType(sMatch, true); if (nLinkType == -1) { return false; } if (this.bAllLinkified) { if (((this.aRegExpDesc[nLinkType] == "HTTP") && this.bLinkify_HTTP) || ((this.aRegExpDesc[nLinkType] == "WWW") && this.bLinkify_WWW) || ((this.aRegExpDesc[nLinkType] == "Other") && this.bLinkify_Other) || ((this.aRegExpDesc[nLinkType] == "StandaloneIP") && this.bLinkify_IP) || ((this.aRegExpDesc[nLinkType] == "Email") && this.bLinkify_Email)) { return false; } } var sLinkLocation = this.Linkify_GetLinkLocation(nLinkType, sMatch); if (sLinkLocation == "") { return false; } return this.Linkify_ClickLink(sLinkLocation, 0, false); } return false; }, Linkify_InitPopup: function() { var ndPopup = document.getElementById("linkifypopup"); if (!ndPopup) { return false; } if (!this.bLinkify_Popup) { var ndParent = ndPopup.parentNode; ndParent.removeChild(ndPopup); return true; } ndPopup.setAttribute("label", "Unlinkify Text (Ctrl+Shift+L)"); if (!this.bAllLinkified) { ndPopup.setAttribute("label", "Linkify Text (Ctrl+Shift+L)"); } } }; function Linkify_ClickWindow(e) { var ndTarget = e.originalTarget; while ((ndTarget.nodeName.toLowerCase() != "a") && (ndTarget.nodeName != "#document")) { ndTarget = ndTarget.parentNode; } if (ndTarget.nodeName.toLowerCase() != "a") { return true; } var sHREF = ndTarget.getAttribute("href"); var sClass = ndTarget.getAttribute("class"); var sTagName = ndTarget.nodeName.toLowerCase(); if ((sTagName != "a") || (sClass != "linkification-ext")) { return true; } if (((e.button == 0) || (e.button == 1)) && !e.shiftKey && !e.altKey && !e.ctrlKey) { var sProtocol = sHREF.substr(0, sHREF.indexOf(":")).toLowerCase(); if ((sProtocol != "http") && (sProtocol != "https") && (sProtocol != "ftp")) { return true; } if (objLinkify.Linkify_ClickLink(sHREF, e.button, true)) { markLinkVisited(sHREF, ndTarget); e.preventDefault(); } } return true; } function Linkify_DoubleClickWindow(e) { var ndDblClicked = e.originalTarget; if ((ndDblClicked.nodeName == "tab") || (ndDblClicked.nodeName == "xul:spacer") || (ndDblClicked.nodeName == "toolbar")) { return false; } var objSelection = window._content.getSelection(); if (!objSelection || objSelection.isCollapsed || !objSelection.anchorNode || (objSelection.anchorNode.nodeName != "#text")) { return false; } objLinkify.Linkify_DoubleClickLink(objSelection); } function Linkify_LoadPopup() { objLinkify.Linkify_InitPopup(); return true; } function Linkify_LoadWindow(e) { this.bFromPopup = false; objLinkify.Linkify_Init(); objLinkify.Linkify_Process(e.originalTarget); var ndMainPopup = document.getElementById("contentAreaContextMenu"); if (ndMainPopup) { ndMainPopup.addEventListener("popupshowing", Linkify_LoadPopup, true); } window.addEventListener("click", Linkify_ClickWindow, true); window.addEventListener("dblclick", Linkify_DoubleClickWindow, true); return true; } window.addEventListener('load', Linkify_LoadWindow, true);