本帖最后由 qjahz 于 2009-8-20 05:54 编辑
大概有一阵子没有发书签脚本了吧- javascript:(function(){
- var snapLinks = {
- init : function(){
- this.flag = null;
- this.box = document.createElement('div');
- this.bs = this.box.style;
- this.bs.opacity = '0.3';
- this.bs.backgroundColor = 'blue';
- this.bs.position = 'absolute';
- this.bs.zIndex = '2147483647';
- this.defaultCursor = getComputedStyle(document.body, '').cursor;
- document.body.style.cursor = 'crosshair';
- document.addEventListener('mousedown', this, true);
- document.addEventListener('mousemove', this ,true);
- document.addEventListener('mouseup', this ,true);
- document.addEventListener('click', this, true);
- },
- uninit : function(){
- document.removeEventListener('mousedown', this, true);
- document.removeEventListener('mousemove', this, true);
- document.removeEventListener('mouseup', this, true);
- document.removeEventListener('click', this, true);
- document.body.removeChild(this.box);
- document.body.style.cursor = this.defaultCursor;
- },
- handleEvent : function(event){
- if (event.button != 0)
- return false;
- event.preventDefault();
- event.stopPropagation();
- switch(event.type){
- case 'mousedown':
- this.downX = event.pageX;
- this.downY = event.pageY;
- this.bs.left = this.downX + 'px';
- this.bs.top = this.downY + 'px';
- document.body.appendChild(this.box);
- this.flag = true;
- break;
- case 'mousemove':
- if (!this.flag) 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.shiftKey){
- this.checkCommand();
- }else{
- this.linkCommand(event.ctrlKey);
- }
- this.uninit();
- break;
- }
- },
- linkCommand : function(isAlert){
- var elems = this.getElements(document.links);
- if (elems.length == 0)
- return;
- if (isAlert){
- if (window.opera){
- prompt(elems.length + ' Links', elems.join('\n'));
- }else{
- alert(elems.join('\n'));
- }
- return;
- }
- elems.forEach(function(elem){
- window.open(elem.href);
- });
- },
- checkCommand : function(){
- var elems = this.getElements(document.getElementsByTagName('input'));
- if (elems.length == 0)
- return;
- elems.forEach(function(elem){
- if (elem.type == 'checkbox' || elem.type == 'radio')
- elem.checked = !elem.checked;
- });
- },
- isInBox : function(elem){
- if (/^(javascript|#)/i.test(elem.getAttribute("href")))
- return false;
- var style = getComputedStyle(elem, null);
- if (style.visibility === 'hidden' || style.display === 'none' || style.opacity === '0')
- return false;
- var rect = elem.getBoundingClientRect();
- if (this.pos.left <= (rect.right + this.pos.scrX) && this.pos.right >= (rect.left + this.pos.scrX))
- if (this.pos.top <= (rect.bottom + this.pos.scrY) && this.pos.bottom >= (rect.top + this.pos.scrY))
- return true;
- return false;
- },
- getElements : function(elems){
- var body = document.body;
- var html = document.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.prototype.filter.call(elems, this.isInBox, this);
- },
- };
- snapLinks.init();
- })();
复制代码
 |