Created
August 21, 2009 09:46
-
-
Save Griever/171798 to your computer and use it in GitHub Desktop.
SnapLinks。keyconfig や FireGestures で snapLinks() を実行
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<!DOCTYPE overlay> | |
<!-- | |
// ==UserScript== | |
// @name SnapLinks.uc.xul | |
// @namespace http://d.hatena.ne.jp/Griever/ | |
// @description Snap Links for userChrome.js | |
// @version 8/21 | |
// @include main | |
// ==/UserScript== | |
--> | |
<overlay id="mainKeyset_overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> | |
<popupset id="mainPopupSet"> | |
<menupopup id="snapLinksMenupopup"> | |
<menuitem label="リンクを新しいタブで開く" oncommand="snap.openLinks();"/> | |
<menuitem label="画像リンクを新しいタブで開く" oncommand="snap.openLinks(/\.(jpe?g|png|gif|bmp)$/i);"/> | |
<menuitem label="画像リンクをひとつのタブで開く" oncommand="snap.imageOnePage();"/> | |
<menuitem label="リンクの URL をコピー" oncommand="snap.copyLinks();"/> | |
<menuitem label="リンクされたファイルの URL をコピー" oncommand="snap.copyLinks(/\.(jpe?g|png|gif|bmp|wmv|mpe?g|flv|avi|asf|mp3|mp4|m4a|wma|wav)$/i);"/> | |
<menuseparator/> | |
<menuitem label="チェックボックスを ON にする" oncommand="snap.checkbox(true);"/> | |
<menuitem label="チェックボックスを OFF にする" oncommand="snap.checkbox(false);"/> | |
<menuitem label="チェックボックスを ON/OFF 入れ替える" oncommand="snap.toggleCheckbox();"/> | |
</menupopup> | |
</popupset> | |
<script type="application/x-javascript" xmlns="http://www.w3.org/1999/xhtml"><![CDATA[ | |
var snap = null; | |
function snapLinks(){ | |
if (snap != null && snap.snapFlag) return; | |
snap = new snapLinks_ucjs(); | |
} | |
function snapLinks_ucjs(){ | |
this.popup = document.getElementById('snapLinksMenupopup'); | |
this.downFlag = false; | |
this.snapFlag = false; | |
this.browser = gBrowser.selectedBrowser; | |
this.addListener(); | |
} | |
snapLinks_ucjs.prototype = { | |
addListener : function(){ | |
this.snapFlag = true; | |
this.browser.addEventListener('mousedown', this, true); | |
this.browser.addEventListener('mousemove', this, true); | |
this.browser.addEventListener('mouseup', this, true); | |
this.browser.addEventListener('click', this, true); | |
this.browser.addEventListener('unload', this, true); | |
}, | |
removeListener : function(){ | |
this.browser.removeEventListener('mousedown', this, true); | |
this.browser.removeEventListener('mousemove', this, true); | |
this.browser.removeEventListener('mouseup', this, true); | |
this.browser.removeEventListener('click', this, true); | |
this.browser.removeEventListener('unload', this, true); | |
}, | |
handleEvent : function(event){ | |
switch(event.type){ | |
case 'click': | |
if (event.button != 0) return false; | |
event.preventDefault(); | |
event.stopPropagation(); | |
break; | |
case 'mousedown': | |
if (event.button != 0) return false; | |
event.preventDefault(); | |
event.stopPropagation(); | |
this.doc = event.target.ownerDocument; | |
this.body = this.doc.body; | |
this.defaultCursor = getComputedStyle(this.body, '').cursor; | |
this.body.style.cursor = 'crosshair'; | |
this.createBox(); | |
this.downX = event.pageX; | |
this.downY = event.pageY; | |
this.bs.left = this.downX + 'px'; | |
this.bs.top = this.downY + 'px'; | |
this.body.appendChild(this.box); | |
this.downFlag = true; | |
break; | |
case 'mousemove': | |
if (!this.downFlag) return; | |
this.moveX = event.pageX; | |
this.moveY = event.pageY; | |
if (this.downX > this.moveX) this.bs.left = this.moveX + 'px'; | |
if (this.downY > this.moveY) this.bs.top = this.moveY + 'px'; | |
this.bs.width = Math.abs(this.moveX - this.downX) + 'px'; | |
this.bs.height = Math.abs(this.moveY - this.downY) + 'px'; | |
break; | |
case 'mouseup': | |
if (event.button != 0) return false; | |
this.popup.addEventListener('popuphiding', function(event){ | |
event.currentTarget.removeEventListener(event.type, arguments.callee, false); | |
snap.removeBox(); | |
setTimeout(function(){ snap = null }, 0); | |
}, false); | |
this.popup.openPopupAtScreen(event.screenX, event.screenY, false); | |
this.removeListener(); | |
break; | |
case 'unload': | |
this.removeBox(); | |
this.removeListener(); | |
break; | |
} | |
}, | |
createBox : function(){ | |
this.box = this.doc.createElement('div'); | |
this.bs = this.box.style; | |
this.bs.cssText = 'opacity:.3; background-color:blue; position:absolute; z-index:2147483647;'; | |
}, | |
removeBox : function(){ | |
if (this.downFlag) this.body.removeChild(this.box); | |
this.body.style.cursor = ''; | |
this.downFlag = false; | |
this.snapFlag = false; | |
}, | |
inBox : function(elem){ | |
var style = getComputedStyle(elem, null); | |
if (style.visibility == 'hidden' || style.display == 'none' || style.opacity == '0') return false; | |
var rect = elem.clientHeight == 0 && elem.firstElementChild? | |
elem.firstElementChild.getBoundingClientRect() : | |
elem.getBoundingClientRect(); | |
return ((this.pos.left <= (rect.right + this.pos.scrX) && this.pos.right >= (rect.left + this.pos.scrX)) | |
&& (this.pos.top <= (rect.bottom + this.pos.scrY) && this.pos.bottom >= (rect.top + this.pos.scrY))); | |
}, | |
getElements : function(nodeList){ | |
var body = this.body; | |
var html = this.doc.documentElement; | |
this.pos = { | |
left : parseInt(this.bs.left), | |
top : parseInt(this.bs.top), | |
right : parseInt(this.bs.left) + parseInt(this.bs.width), | |
bottom : parseInt(this.bs.top) + parseInt(this.bs.height), | |
scrX : (body.scrollLeft || html.scrollLeft)- html.clientLeft, | |
scrY : (body.scrollTop || html.scrollTop) - html.clientTop, | |
}; | |
return Array.filter(nodeList, this.inBox, this); | |
}, | |
openLinks : function(regexp){ | |
var links = snap.getElements(snap.doc.links); | |
for (var i = 0, l = links.length; i < l; i++){ | |
var elem = links[i]; | |
if (typeof regexp == 'undefined'){ | |
if (!/^javascript\:|\#/i.test(elem.href)) gBrowser.addTab(elem.href); | |
}else if (regexp.test(elem.href)){ | |
gBrowser.addTab(elem.href); | |
} | |
} | |
}, | |
copyLinks : function(regexp){ | |
var links = snap.getElements(snap.doc.links); | |
for (var i = 0, l = links.length, h = []; i < l; i++){ | |
var elem = links[i]; | |
h.push(elem.href); | |
} | |
if (h.length){ | |
Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper).copyString(h.join('\n')); | |
document.getElementById('statusbar-display').setAttribute('label', h.length + ' 個コピーしました'); | |
} | |
}, | |
imageOnePage : function(){ | |
var src = snap.getElements(snap.doc.links).map(function(elem){ | |
return /\.(jpe?g|png|gif|bmp)$/i.test(elem.href)? '<img src="' + elem.href + '">' : ''; | |
}); | |
gBrowser.addTab("data:text/html," + escape(src.join('\n'))); | |
}, | |
checkbox : function(check){ | |
var checkbox = snap.getElements(snap.body.querySelectorAll('input[type="checkbox"]')); | |
for (var i = 0, l = checkbox.length; i < l; i++){ | |
var elem = checkbox[i]; | |
elem.checked = check; | |
} | |
}, | |
toggleCheckbox : function(){ | |
var checkbox = snap.getElements(snap.body.querySelectorAll('input[type="checkbox"]')); | |
for (var i = 0, l = checkbox.length; i < l; i++){ | |
var elem = checkbox[i]; | |
elem.checked = !elem.checked; | |
} | |
}, | |
}; | |
]]></script> | |
</overlay> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The idea looks cool, can you help me understand what it is and how to implement it?
Does it replace Snap Links, as a firegesture script, thus being more convenient? Or is it supposed to work in tandem with the snaplinks addon?
How would I best implement it?
Thanks!