Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gBrowser = document.getElementById("content");
- gBrowser.addEventListener("click", SMConClick, true);
- function fixupAURL(aURL) {
- var str = '^(https?\:|ftp\:|news\:|mailto\:|smb\:' +
- 'gopher\:|about\:|file\:|cache\:|javascript\:|' +
- 'x\-jsd\:|jar\:|resource\:|data\:)'
- var re = new RegExp(str, 'i');
- if (re.test(aURL)) {
- return aURL;
- }
- else {
- var urifix = Components.classes['@mozilla.org/docshell/urifixup;1'].getService(Components.interfaces.nsIURIFixup);
- var loc = urifix.createFixupURI(getBrowser().currentURI.spec, 0);
- var host = loc.host, path = loc.path, proto = loc.scheme;
- if (aURL[0] == '/') {
- path = '';
- }
- else {
- path = path.replace(/(.+?\/?)[^\/]+?$/, "$1");
- while (/\.\.\//i.test(aURL)) {
- aURL = aURL.replace(/\.\.\//i, '');
- path = path.replace(/(.+?\/?)[^\/]+?\/$|([^\/]+?\/?)$/, "$1");
- }
- if (path[0] != '/') {
- path = '/' + path;
- }
- }
- return proto + '://' + host + path + aURL;
- }
- }
- function SMConClick(aEvent) {
- if ( (aEvent.button == 1 || (aEvent.button == 0 && (aEvent.ctrlKey || aEvent.shiftKey)))
- && contentAreaClick2(aEvent) )
- aEvent.stopPropagation();
- }
- function contentAreaClick2(event)
- {
- var target = event.target;
- var linkNode;
- if (target instanceof HTMLAnchorElement ||
- target instanceof HTMLAreaElement ||
- target instanceof HTMLLinkElement)
- {
- if (target.hasAttribute("href"))
- linkNode = target;
- }
- else {
- linkNode = event.originalTarget;
- while (linkNode && !(linkNode instanceof HTMLAnchorElement))
- linkNode = linkNode.parentNode;
- // <a> cannot be nested. So if we find an anchor without an
- // href, there is no useful <a> around the target
- if (linkNode && !linkNode.hasAttribute("href"))
- linkNode = null;
- }
- var wrapper = null;
- if (linkNode) {
- wrapper = linkNode;
- var is_js = wrapper.href.indexOf('javascript:');
- var pound = wrapper.href.charAt(wrapper.href.length-1);
- // Check if URL has 'javascript:' in it
- if (is_js != -1) {
- var jsParts = wrapper.href.split("'");
- rawHref = jsParts[1];
- if (rawHref) {
- newHref = unescape(fixupAURL(rawHref));
- handleLinkClick(event, newHref, linkNode);
- }
- // If extraction fails, try the onclick attribute
- else if (wrapper.getAttribute("onclick")) {
- var ocParts = wrapper.getAttribute("onclick").split("'");
- rawHref = ocParts[1];
- if (rawHref) {
- newHref = unescape(fixupAURL(rawHref));
- handleLinkClick(event, newHref, linkNode);
- }
- }
- return true;
- }
- // Check if URL ends in '#'
- else if (pound == '#') {
- var ocParts = wrapper.getAttribute("onclick").split("'");
- rawHref = ocParts[1];
- if (rawHref) {
- newHref = unescape(fixupAURL(rawHref));
- handleLinkClick(event, newHref, linkNode);
- }
- return true;
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement