level 1
8点七十八
楼主
"use strict";
Tabmix.contentAreaClick = {
_data: null,
getPref: function() {
XPCOMUtils.defineLazyGetter(this, "targetPref", function() {
return Tabmix.prefs.getIntPref("opentabforLinks");
});
XPCOMUtils.defineLazyGetter(this, "currentTabLocked", function() {
return gBrowser.mCurrentTab.hasAttribute("locked");
});
},
/*
* @param event A valid event union.
* @param href href string.
* @param linkNode The DOM node containing the URL to be opened.
* @param targetAttr The target attribute of the link node.
*/
getData: function(event, href, linkNode, targetAttr) {
let self = this;
function LinkData() {
this.event = event;
this.href = href;
this.linkNode = linkNode;
this.targetAttr = targetAttr;
XPCOMUtils.defineLazyGetter(this, "onclick", function() {
if (linkNode.hasAttribute("onclick"))
return linkNode.getAttribute("onclick");
return null;
});
XPCOMUtils.defineLazyGetter(this, "isLinkToExternalDomain", function() {
/*
* Check if link refers to external domain.
* Get current page url
* if user click a link while the page is reloading linkNode.ownerDocument.location can be null
*/
let location = linkNode.ownerDocument.location;
let curpage = location ? location.href || location.baseURI : gBrowser.currentURI.spec;
return self.isLinkToExternalDomain(curpage, window.XULBrowserWindow.overLink || linkNode);
});
}
this._data = new LinkData();
},
init: function TMP_CA_init() {
Tabmix.changeCode(window, "contentAreaClick")._replace(
'if (linkNode &&',
'var targetAttr = Tabmix.contentAreaClick.getTargetAttr(linkNode);' +
'var [where, suppressTabsOnFileDownload] =' +
' Tabmix.contentAreaClick.whereToOpen(event, linkNode, href, targetAttr);' +
'Tabmix.contentAreaClick._data = null;' +
'if (where == "current") gBrowser.mCurrentBrowser.tabmix_allowLoad = true;' +
'else if (event.__href) href = event.__href;' +
'$&'
)._replace(
'if (linkNode.getAttribute("onclick")',
'if (where == "default") $&'
)._replace(
'loadURI(',
' if (where == "tab" || where == "tabshifted") {' +
' let doc = event.target.ownerDocument;' +
' let _url = Tabmix.isVersion(190) ? href : url;' +
' openLinkIn(_url, where, {referrerURI: doc.documentURIObject, charset: doc.characterSet,' +
' initiatingDoc: doc,' +
' suppressTabsOnFileDownload: suppressTabsOnFileDownload});' +
' }' +
' else $&'
)._replace(
// force handleLinkClick to use openLinkIn by replace "current"
// with " current", we later use trim() before handleLinkClick call openLinkIn
'handleLinkClick(event, href, linkNode);',
' if (event.button == 1 && Tabmix.contentAreaClick.getHrefFromOnClick(event, href, linkNode)) {' +
' href = event.__href;' +
' where = "tab";' +
' }' +
' event.__where = where == "current" && href.indexOf("custombutton://") != 0 ? " " + where : where;' +
' event.__suppressTabsOnFileDownload = suppressTabsOnFileDownload;' +
' var result = $&' +
' if (targetAttr == "_new" && !result) Tabmix.contentAreaClick.selectExistingTab(href);'
).toCode();
/* don't change where if it is save, window, or we passed
* event.__where = default from contentAreaClick or
* Tabmix.contentAreaClick.contentLinkClick
*/
Tabmix.changeCode(window, "handleLinkClick")._replace(
'whereToOpenLink(event);',
'$&' +
' if (event && event.__where && event.__where != "default" &&' +
' ["tab","tabshifted","current"].indexOf(where) != -1) {' +
' where = event.__where.split(".")[0];' +
' }'
)._replace(
'var doc = event.target.ownerDocument;',
'where = where.trim();\
$&'
)._replace(
'charset: doc.characterSet',
'$&, suppressTabsOnFileDownload: event.__suppressTabsOnFileDownload'
).toCode();
},
whereToOpen: function TMP_CA_whereToOpen(event, linkNode, href, targetAttr) {
function TMP_tabshifted(event) {
var where = whereToOpenLink(event);
return where == "tabshifted" ? "tabshifted" : "tab";
}
///XXX check again how SubmitToTab work
if (typeof(SubmitToTab) != 'undefined') {
let target = event.target;
if (target instanceof HTMLButtonElement ||
target instanceof HTMLInputElement) {
if (SubmitToTab.contentAreaClick(event) == false) {
return ["default"];
}
}
}
if (!linkNode)
return ["default"];
this.getPref();
this.getData(event, href, linkNode, targetAttr);
/*
* prevents tab form opening when clicking Greasemonkey script
*/
if (this.isGreasemonkeyScript(href))
return ["default"];
// Check if new tab already opened from onclick event // 2006-09-26
if (this._data.onclick && gBrowser.contentDocument.location.href != document.commandDispatcher.focusedWindow.top.location.href)
return ["default"];
if (linkNode.getAttribute("rel") == "sidebar" || targetAttr == "_search" ||
href.indexOf("mailto:") > -1) {
return ["default"];
}
/*
* prevent tabs from opening if left-clicked link ends with given filetype or matches regexp;
* portions were taken from disable target for downloads by cusser
*/
if (this.suppressTabsOnFileDownload()) {
// don't do anything if we are on gmail and let gmail take care of the download
let url = gBrowser.currentURI ? gBrowser.currentURI.spec : "";
let isGmail = /^(http|https):\/\/mail.google.com/.test(url);
let isHttps = /^https/.test(href);
if (isGmail || isHttps)
return ["default", true];
return ["current", true];
}
if (!/^(http|about)/.test(linkNode.protocol))
return ["default"];
// check this after we check for suppressTabsOnFileDownload
// for the case the link have a matche in our list
if (typeof event.tabmix_openLinkWithHistory == "boolean")
return ["current"];
// don't mess with links that have onclick inside iFrame
let onClickInFrame = this._data.onclick && linkNode.ownerDocument.defaultView.frameElement;
/*
* force a middle-clicked link to open in the current tab if certain conditions
* are true. See the function comment for more details.
*/
if (this.divertMiddleClick()) {
return [onClickInFrame ? "current.frame" : "current"];
}
if (onClickInFrame)
return ["default"];
// catch other middle & right click
if (event.button != 0)
return ["default"];
// the rest of the code if for left-click only
/*
* don't change default behavior for links that point to exiting frame
* in the current page
*/
if (this.targetIsFrame())
return ["default"];
/*
* open targeted links in the current tab only if certain conditions are met.
* See the function comment for more details.
*/
if (this.divertTargetedLink())
return ["current"];
/*
* open links to other sites in a tab only if certain conditions are met. See the
* function comment for more details.
*/
if (this.openExSiteLink())
return [TMP_tabshifted(event)];
if (this.currentTabLocked || this.targetPref == 1) { // tab is locked
let openNewTab = this.openTabfromLink();
if (openNewTab != null)
return [openNewTab ? TMP_tabshifted(event) : "current"];
}
return ["default"];
},
/**
* @brief Handle left-clicks on links when preference is to open new tabs from links
* links that are not handled here go on to the page code and then to contentAreaClick
*/
_contentLinkClick: function TMP__contentLinkClick(aEvent) {
Tabmix.contentAreaClick.contentLinkClick(aEvent);
Tabmix.contentAreaClick._data = null;
},
contentLinkClick: function TMP_contentLinkClick(aEvent) {
if (typeof aEvent.tabmix_openLinkWithHistory == "boolean")
return;
if (aEvent.button != 0 || aEvent.shiftKey || aEvent.ctrlKey || aEvent.altKey || aEvent.metaKey)
return;
this.getPref();
if (!this.currentTabLocked && this.targetPref == 0)
return;
let [href, linkNode] = hrefAndLinkNodeForClickEvent(aEvent);
if (!linkNode || !/^(http|about)/.test(linkNode.protocol))
return;
let targetAttr = this.getTargetAttr(linkNode);
this.getData(aEvent, href, linkNode, targetAttr);
var currentHref = gBrowser.currentURI ? gBrowser.currentURI.spec : "";
try {
// for the moment just do it for Google and Yahoo....
// and tvguide.com - added 2013-07-20
var blocked = /tvguide.com|google|yahoo.com\/search|my.yahoo.com/.test(currentHref);
} catch (ex) {blocked = false;}
if (!blocked) {
// replace onclick function with the form javascript:top.location.href = url
// if the tab is locked or we force new tab from link
let {onclick} = this._data;
if ((this.currentTabLocked || this.targetPref == 1) && onclick) {
let code = "javascript:top.location.href="
if (this.checkAttr(href, "javascript:void(0)") && this.checkAttr(onclick, code))
linkNode.setAttribute("onclick", onclick.replace(code, "var __tabmix.href="));
}
return;
}
// don't do anything on mail.google or google.com/reader
var isGmail = /^(http|https):\/\/mail.google.com/.test(currentHref) || /^(http|https):\/\/\w*.google.com\/reader/.test(currentHref);
if (isGmail)
return;
if ("className" in linkNode) {
// don't interrupt with noscript
if (linkNode.className.indexOf("__noscriptPlaceholder__") > -1)
return;
// need to find a way to work here only on links
if (/button/.test(linkNode.className.toLowerCase()))
return;
}
// don't interrupt with fastdial links
if ("ownerDocument" in linkNode && Tabmix.isNewTabUrls(linkNode.ownerDocument.documentURI))
return;
if (linkNode.getAttribute("rel") == "sidebar" || targetAttr == "_search" ||
href.indexOf("mailto:") > -1)
return;
/*
* prevents tab form opening when clicking Greasemonkey script
*/
if (this.isGreasemonkeyScript(href))
return;
/*
* prevent tabs from opening if left-clicked link ends with given filetype or matches regexp;
* portions were taken from disable target for downloads by cusser
*/
if (this.suppressTabsOnFileDownload())
return;
// don't mess with links that have onclick inside iFrame
if (this._data.onclick && linkNode.ownerDocument.defaultView.frameElement)
return;
/*
* don't change default behavior for links that point to exiting frame
* in the current page
*/
if (this.targetIsFrame())
return;
/*
* open targeted links in the current tab only if certain conditions are met.
* See the function comment for more details.
*/
if (this.divertTargetedLink())
return;
// open links to other sites in a tab only if certain conditions are met. See the
// function comment for more details.
var openNewTab = null;
if (this.openExSiteLink())
openNewTab = true;
// when a tab is locked or preference is to open in new tab
// we check that link is not a Javascript or have a onclick function
else if (this.currentTabLocked || this.targetPref == 1)
openNewTab = this.openTabfromLink();
if (openNewTab) {
let where = whereToOpenLink(aEvent);
aEvent.__where = where == "tabshifted" ? "tabshifted" : "tab";
handleLinkClick(aEvent, href, linkNode);
aEvent.stopPropagation();
aEvent.preventDefault();
}
},
2014年01月18日 16点01分
1
Tabmix.contentAreaClick = {
_data: null,
getPref: function() {
XPCOMUtils.defineLazyGetter(this, "targetPref", function() {
return Tabmix.prefs.getIntPref("opentabforLinks");
});
XPCOMUtils.defineLazyGetter(this, "currentTabLocked", function() {
return gBrowser.mCurrentTab.hasAttribute("locked");
});
},
/*
* @param event A valid event union.
* @param href href string.
* @param linkNode The DOM node containing the URL to be opened.
* @param targetAttr The target attribute of the link node.
*/
getData: function(event, href, linkNode, targetAttr) {
let self = this;
function LinkData() {
this.event = event;
this.href = href;
this.linkNode = linkNode;
this.targetAttr = targetAttr;
XPCOMUtils.defineLazyGetter(this, "onclick", function() {
if (linkNode.hasAttribute("onclick"))
return linkNode.getAttribute("onclick");
return null;
});
XPCOMUtils.defineLazyGetter(this, "isLinkToExternalDomain", function() {
/*
* Check if link refers to external domain.
* Get current page url
* if user click a link while the page is reloading linkNode.ownerDocument.location can be null
*/
let location = linkNode.ownerDocument.location;
let curpage = location ? location.href || location.baseURI : gBrowser.currentURI.spec;
return self.isLinkToExternalDomain(curpage, window.XULBrowserWindow.overLink || linkNode);
});
}
this._data = new LinkData();
},
init: function TMP_CA_init() {
Tabmix.changeCode(window, "contentAreaClick")._replace(
'if (linkNode &&',
'var targetAttr = Tabmix.contentAreaClick.getTargetAttr(linkNode);' +
'var [where, suppressTabsOnFileDownload] =' +
' Tabmix.contentAreaClick.whereToOpen(event, linkNode, href, targetAttr);' +
'Tabmix.contentAreaClick._data = null;' +
'if (where == "current") gBrowser.mCurrentBrowser.tabmix_allowLoad = true;' +
'else if (event.__href) href = event.__href;' +
'$&'
)._replace(
'if (linkNode.getAttribute("onclick")',
'if (where == "default") $&'
)._replace(
'loadURI(',
' if (where == "tab" || where == "tabshifted") {' +
' let doc = event.target.ownerDocument;' +
' let _url = Tabmix.isVersion(190) ? href : url;' +
' openLinkIn(_url, where, {referrerURI: doc.documentURIObject, charset: doc.characterSet,' +
' initiatingDoc: doc,' +
' suppressTabsOnFileDownload: suppressTabsOnFileDownload});' +
' }' +
' else $&'
)._replace(
// force handleLinkClick to use openLinkIn by replace "current"
// with " current", we later use trim() before handleLinkClick call openLinkIn
'handleLinkClick(event, href, linkNode);',
' if (event.button == 1 && Tabmix.contentAreaClick.getHrefFromOnClick(event, href, linkNode)) {' +
' href = event.__href;' +
' where = "tab";' +
' }' +
' event.__where = where == "current" && href.indexOf("custombutton://") != 0 ? " " + where : where;' +
' event.__suppressTabsOnFileDownload = suppressTabsOnFileDownload;' +
' var result = $&' +
' if (targetAttr == "_new" && !result) Tabmix.contentAreaClick.selectExistingTab(href);'
).toCode();
/* don't change where if it is save, window, or we passed
* event.__where = default from contentAreaClick or
* Tabmix.contentAreaClick.contentLinkClick
*/
Tabmix.changeCode(window, "handleLinkClick")._replace(
'whereToOpenLink(event);',
'$&' +
' if (event && event.__where && event.__where != "default" &&' +
' ["tab","tabshifted","current"].indexOf(where) != -1) {' +
' where = event.__where.split(".")[0];' +
' }'
)._replace(
'var doc = event.target.ownerDocument;',
'where = where.trim();\
$&'
)._replace(
'charset: doc.characterSet',
'$&, suppressTabsOnFileDownload: event.__suppressTabsOnFileDownload'
).toCode();
},
whereToOpen: function TMP_CA_whereToOpen(event, linkNode, href, targetAttr) {
function TMP_tabshifted(event) {
var where = whereToOpenLink(event);
return where == "tabshifted" ? "tabshifted" : "tab";
}
///XXX check again how SubmitToTab work
if (typeof(SubmitToTab) != 'undefined') {
let target = event.target;
if (target instanceof HTMLButtonElement ||
target instanceof HTMLInputElement) {
if (SubmitToTab.contentAreaClick(event) == false) {
return ["default"];
}
}
}
if (!linkNode)
return ["default"];
this.getPref();
this.getData(event, href, linkNode, targetAttr);
/*
* prevents tab form opening when clicking Greasemonkey script
*/
if (this.isGreasemonkeyScript(href))
return ["default"];
// Check if new tab already opened from onclick event // 2006-09-26
if (this._data.onclick && gBrowser.contentDocument.location.href != document.commandDispatcher.focusedWindow.top.location.href)
return ["default"];
if (linkNode.getAttribute("rel") == "sidebar" || targetAttr == "_search" ||
href.indexOf("mailto:") > -1) {
return ["default"];
}
/*
* prevent tabs from opening if left-clicked link ends with given filetype or matches regexp;
* portions were taken from disable target for downloads by cusser
*/
if (this.suppressTabsOnFileDownload()) {
// don't do anything if we are on gmail and let gmail take care of the download
let url = gBrowser.currentURI ? gBrowser.currentURI.spec : "";
let isGmail = /^(http|https):\/\/mail.google.com/.test(url);
let isHttps = /^https/.test(href);
if (isGmail || isHttps)
return ["default", true];
return ["current", true];
}
if (!/^(http|about)/.test(linkNode.protocol))
return ["default"];
// check this after we check for suppressTabsOnFileDownload
// for the case the link have a matche in our list
if (typeof event.tabmix_openLinkWithHistory == "boolean")
return ["current"];
// don't mess with links that have onclick inside iFrame
let onClickInFrame = this._data.onclick && linkNode.ownerDocument.defaultView.frameElement;
/*
* force a middle-clicked link to open in the current tab if certain conditions
* are true. See the function comment for more details.
*/
if (this.divertMiddleClick()) {
return [onClickInFrame ? "current.frame" : "current"];
}
if (onClickInFrame)
return ["default"];
// catch other middle & right click
if (event.button != 0)
return ["default"];
// the rest of the code if for left-click only
/*
* don't change default behavior for links that point to exiting frame
* in the current page
*/
if (this.targetIsFrame())
return ["default"];
/*
* open targeted links in the current tab only if certain conditions are met.
* See the function comment for more details.
*/
if (this.divertTargetedLink())
return ["current"];
/*
* open links to other sites in a tab only if certain conditions are met. See the
* function comment for more details.
*/
if (this.openExSiteLink())
return [TMP_tabshifted(event)];
if (this.currentTabLocked || this.targetPref == 1) { // tab is locked
let openNewTab = this.openTabfromLink();
if (openNewTab != null)
return [openNewTab ? TMP_tabshifted(event) : "current"];
}
return ["default"];
},
/**
* @brief Handle left-clicks on links when preference is to open new tabs from links
* links that are not handled here go on to the page code and then to contentAreaClick
*/
_contentLinkClick: function TMP__contentLinkClick(aEvent) {
Tabmix.contentAreaClick.contentLinkClick(aEvent);
Tabmix.contentAreaClick._data = null;
},
contentLinkClick: function TMP_contentLinkClick(aEvent) {
if (typeof aEvent.tabmix_openLinkWithHistory == "boolean")
return;
if (aEvent.button != 0 || aEvent.shiftKey || aEvent.ctrlKey || aEvent.altKey || aEvent.metaKey)
return;
this.getPref();
if (!this.currentTabLocked && this.targetPref == 0)
return;
let [href, linkNode] = hrefAndLinkNodeForClickEvent(aEvent);
if (!linkNode || !/^(http|about)/.test(linkNode.protocol))
return;
let targetAttr = this.getTargetAttr(linkNode);
this.getData(aEvent, href, linkNode, targetAttr);
var currentHref = gBrowser.currentURI ? gBrowser.currentURI.spec : "";
try {
// for the moment just do it for Google and Yahoo....
// and tvguide.com - added 2013-07-20
var blocked = /tvguide.com|google|yahoo.com\/search|my.yahoo.com/.test(currentHref);
} catch (ex) {blocked = false;}
if (!blocked) {
// replace onclick function with the form javascript:top.location.href = url
// if the tab is locked or we force new tab from link
let {onclick} = this._data;
if ((this.currentTabLocked || this.targetPref == 1) && onclick) {
let code = "javascript:top.location.href="
if (this.checkAttr(href, "javascript:void(0)") && this.checkAttr(onclick, code))
linkNode.setAttribute("onclick", onclick.replace(code, "var __tabmix.href="));
}
return;
}
// don't do anything on mail.google or google.com/reader
var isGmail = /^(http|https):\/\/mail.google.com/.test(currentHref) || /^(http|https):\/\/\w*.google.com\/reader/.test(currentHref);
if (isGmail)
return;
if ("className" in linkNode) {
// don't interrupt with noscript
if (linkNode.className.indexOf("__noscriptPlaceholder__") > -1)
return;
// need to find a way to work here only on links
if (/button/.test(linkNode.className.toLowerCase()))
return;
}
// don't interrupt with fastdial links
if ("ownerDocument" in linkNode && Tabmix.isNewTabUrls(linkNode.ownerDocument.documentURI))
return;
if (linkNode.getAttribute("rel") == "sidebar" || targetAttr == "_search" ||
href.indexOf("mailto:") > -1)
return;
/*
* prevents tab form opening when clicking Greasemonkey script
*/
if (this.isGreasemonkeyScript(href))
return;
/*
* prevent tabs from opening if left-clicked link ends with given filetype or matches regexp;
* portions were taken from disable target for downloads by cusser
*/
if (this.suppressTabsOnFileDownload())
return;
// don't mess with links that have onclick inside iFrame
if (this._data.onclick && linkNode.ownerDocument.defaultView.frameElement)
return;
/*
* don't change default behavior for links that point to exiting frame
* in the current page
*/
if (this.targetIsFrame())
return;
/*
* open targeted links in the current tab only if certain conditions are met.
* See the function comment for more details.
*/
if (this.divertTargetedLink())
return;
// open links to other sites in a tab only if certain conditions are met. See the
// function comment for more details.
var openNewTab = null;
if (this.openExSiteLink())
openNewTab = true;
// when a tab is locked or preference is to open in new tab
// we check that link is not a Javascript or have a onclick function
else if (this.currentTabLocked || this.targetPref == 1)
openNewTab = this.openTabfromLink();
if (openNewTab) {
let where = whereToOpenLink(aEvent);
aEvent.__where = where == "tabshifted" ? "tabshifted" : "tab";
handleLinkClick(aEvent, href, linkNode);
aEvent.stopPropagation();
aEvent.preventDefault();
}
},