agate
小狐狸
小狐狸
  • UID327
  • 注册日期2004-11-29
  • 最后登录2018-12-23
  • 发帖数72
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
阅读:3436回复:7

请问能否给这个截图脚本加上鼠标手势或快捷键呢?

楼主#
更多 发布于:2010-01-18 12:22
很好用的一个网页截屏脚本,可以把整幅(从头到脚)网页另存为图片
可惜每次要到菜单里去点——我的菜单设置ALT打开的,比较麻烦。
请问各位达人能否给这脚本加上鼠标手势或快捷键?或者另推荐一款脚本也好啊
多谢啦 :)

// ==UserScript==
// @name        Capture Web Page for userChrome.js
// @include     main
// @include     chrome://browser/content/browser.xul
// ==/UserScript==

/*
网页截图 - Griever - http://d.hatena.ne.jp/Griever/20081222/1229958279
を勝手にuserChrome.js化
*/

//ページ全体をキャプチャ
(function(){

	var captureMenu = document.createElement("menuitem");
	captureMenu.setAttribute("label","Capture This Page");
	captureMenu.addEventListener("command",function(){
	    var win = window.content;
	    var w = win.document.width;
	    var h = win.document.height;
	
		var pos = document.getElementById('status-bar');
		var scrollbox = document.createElement('scrollbox');
		scrollbox.width = '1';
		scrollbox.height = '1';
		pos.appendChild(scrollbox);
	
	    var canvas = win.document.createElement('canvas');
	    canvas.style.display = 'inline';
	    canvas.width = w;
	    canvas.height = h;
	    scrollbox.appendChild(canvas);
	
	    var ctx = canvas.getContext("2d");
	    ctx.clearRect(0, 0, canvas.width, canvas.height);
	    ctx.save();
	    ctx.scale(1.0, 1.0);
	    ctx.drawWindow(win, 0, 0, w, h, "rgb(255,255,255)");
	    ctx.restore();
	
	    var url = canvas.toDataURL("image/png");
		const IO_SERVICE = Components.classes['@mozilla.org/network/io-service;1']
                   .getService(Components.interfaces.nsIIOService);
		url = IO_SERVICE.newURI(url, null, null);

		var fp = Components.classes['@mozilla.org/filepicker;1']
		          .createInstance(Components.interfaces.nsIFilePicker);
		fp.init(window, "Save Screenshot As", fp.modeSave);
		fp.appendFilters(fp.filterImages);
		fp.defaultExtension = "png";
		fp.defaultString = win.document.title + ".png";
		if ( fp.show() == fp.returnCancel || !fp.file ) return;

		var wbp = Components.classes['@mozilla.org/embedding/browser/nsWebBrowserPersist;1']
         	 .createInstance(Components.interfaces.nsIWebBrowserPersist);
		wbp.saveURI(url, null, null, null, null, fp.file);

	    pos.removeChild(scrollbox);
	},false);

	document.getElementById("menu_ToolsPopup").insertBefore(
		captureMenu,
		document.getElementById("sanitizeSeparator"));
})();
agate
小狐狸
小狐狸
  • UID327
  • 注册日期2004-11-29
  • 最后登录2018-12-23
  • 发帖数72
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
1楼#
发布于:2010-01-18 12:22
找了个截屏脚本WebScreenShot.uc.xul
可惜在我机器上导致FF死掉……cpu占用50%,内存飙升……也没有快捷、手势之类的……
达人现身啊,呵呵
多谢
xelnaga
千年狐狸
千年狐狸
  • UID1911
  • 注册日期2005-01-08
  • 最后登录2022-04-20
  • 发帖数1725
  • 经验85枚
  • 威望0点
  • 贡献值8点
  • 好评度5点
  • 社区居民
2楼#
发布于:2010-01-18 12:22
方向错了吧
不是在它里面加手势,是在手势里面加上截屏功能
xelnaga
千年狐狸
千年狐狸
  • UID1911
  • 注册日期2005-01-08
  • 最后登录2022-04-20
  • 发帖数1725
  • 经验85枚
  • 威望0点
  • 贡献值8点
  • 好评度5点
  • 社区居民
3楼#
发布于:2010-01-18 12:22
我建议你装screengrab这个扩展
如果你执意装脚本

参看一下这贴
https://www.firefox.net.cn/forum/viewtopic.php?t=29712
以后可以自行解决



--------------------
我把你的截屏功能加到手势脚本里了

注意,修改后的脚本只有截屏一个功能
另外假定你没有其他手势脚本或扩展
否则可能出现无法预计的冲突

手势是 左右左
========

// ==UserScript==
// @name           sgrab
// @namespace      http://www.xuldev.org/
// @include        main
// @compatibility  Firefox 3.5
// @author         Gomita
// @version        1.0.20080201
// @homepage       http://www.xuldev.org/misc/ucjs.php
// ==/UserScript== 

var ucjsMouseGestures = {

   _lastX: 0,
   _lastY: 0,
   _directionChain: "",

   init: function()
   {
      gBrowser.mPanelContainer.addEventListener("mousedown", this, false);
      gBrowser.mPanelContainer.addEventListener("mousemove", this, false);
      gBrowser.mPanelContainer.addEventListener("mouseup", this, false);
      gBrowser.mPanelContainer.addEventListener("contextmenu", this, true);
   },

   uninit: function()
   {
      gBrowser.mPanelContainer.removeEventListener("mousedown", this, false);
      gBrowser.mPanelContainer.removeEventListener("mousemove", this, false);
      gBrowser.mPanelContainer.removeEventListener("mouseup", this, false);
      gBrowser.mPanelContainer.removeEventListener("contextmenu", this, true);
   },

   _isMouseDown: false,
   _suppressContext: false,
   _shouldFireContext: false,   // for Linux

   handleEvent: function(event)
   {
      switch (event.type) {
         case "mousedown":
            if (event.button == 2) {
               this._isMouseDown = true;
               this._startGesture(event);
            }
            break;
         case "mousemove":
            if (this._isMouseDown) {
               this._progressGesture(event);
            }
            break;
         case "mouseup":
            if (this._isMouseDown) {
               this._isMouseDown = false;
               this._suppressContext = !!this._directionChain;
               this._stopGesture(event);
               if (this._shouldFireContext) {
                  this._shouldFireContext = false;
                  this._displayContextMenu(event);
               }
            }
            break;
         case "contextmenu":
            if (this._suppressContext || this._isMouseDown) {
               this._suppressContext = false;
               event.preventDefault();
               event.stopPropagation();
               if (this._isMouseDown) {
                  this._shouldFireContext = true;
               }
            }
            break;
      }
   },

   _displayContextMenu: function(event)
   {
      var evt = event.originalTarget.ownerDocument.createEvent("MouseEvents");
      evt.initMouseEvent(
         "contextmenu", true, true, event.originalTarget.defaultView, 0,
         event.screenX, event.screenY, event.clientX, event.clientY,
         false, false, false, false, 2, null
      );
      event.originalTarget.dispatchEvent(evt);
   },

   _startGesture: function(event)
   {
      this._lastX = event.screenX;
      this._lastY = event.screenY;
      this._directionChain = "";
   },

   _progressGesture: function(event)
   {
      var x = event.screenX;
      var y = event.screenY;
      var distanceX = Math.abs(x - this._lastX);
      var distanceY = Math.abs(y - this._lastY);
      // minimal movement where the gesture is recognized
      const tolerance = 10;
      if (distanceX < tolerance && distanceY < tolerance)
         return;
      // determine current direction
      var direction;
      if (distanceX > distanceY)
         direction = x < this._lastX ? "L" : "R";
      else
         direction = y < this._lastY ? "U" : "D";
      // compare to last direction
      var lastDirection = this._directionChain.charAt(this._directionChain.length - 1);
      if (direction != lastDirection) {
         this._directionChain += direction;
         XULBrowserWindow.statusTextField.label = "Gesture: " + this._directionChain;
      }
      // save current position
      this._lastX = x;
      this._lastY = y;
   },

   _stopGesture: function(event)
   {
      try {
         if (this._directionChain)
            this._performAction(event);
         XULBrowserWindow.statusTextField.label = "";
      }
      catch(ex) {
         XULBrowserWindow.statusTextField.label = ex;
      }
      this._directionChain = "";
   },
   
 // ============================   添加部分============================   
   screengrab: function(){
       var win = window.content;
       var w = win.document.width;
       var h = win.document.height;
   
      var pos = document.getElementById('status-bar');
      var scrollbox = document.createElement('scrollbox');
      scrollbox.width = '1';
      scrollbox.height = '1';
      pos.appendChild(scrollbox);
   
       var canvas = win.document.createElement('canvas');
       canvas.style.display = 'inline';
       canvas.width = w;
       canvas.height = h;
       scrollbox.appendChild(canvas);
   
       var ctx = canvas.getContext("2d");
       ctx.clearRect(0, 0, canvas.width, canvas.height);
       ctx.save();
       ctx.scale(1.0, 1.0);
       ctx.drawWindow(win, 0, 0, w, h, "rgb(255,255,255)");
       ctx.restore();
   
       var url = canvas.toDataURL("image/png");
      const IO_SERVICE = Components.classes['@mozilla.org/network/io-service;1']
                   .getService(Components.interfaces.nsIIOService);
      url = IO_SERVICE.newURI(url, null, null);

      var fp = Components.classes['@mozilla.org/filepicker;1']
                .createInstance(Components.interfaces.nsIFilePicker);
      fp.init(window, "Save Screenshot As", fp.modeSave);
      fp.appendFilters(fp.filterImages);
      fp.defaultExtension = "png";
      fp.defaultString = win.document.title + ".png";
      if ( fp.show() == fp.returnCancel || !fp.file ) return;

      var wbp = Components.classes['@mozilla.org/embedding/browser/nsWebBrowserPersist;1']
             .createInstance(Components.interfaces.nsIWebBrowserPersist);
      wbp.saveURI(url, null, null, null, null, fp.file);

       pos.removeChild(scrollbox);
},
   
   _performAction: function(event)
   {
      // These are the mouse gesture mappings. Customize this as you like.
      switch (this._directionChain) {
         // screengrab
         case "LRL": this.screengrab(); break;
         // Unknown Gesture
         default: throw "Unknown Gesture: " + this._directionChain;
      }
   }

};

// Entry Point
ucjsMouseGestures.init();
window.addEventListener("unload", function(){ ucjsMouseGestures.uninit(); }, false);
agate
小狐狸
小狐狸
  • UID327
  • 注册日期2004-11-29
  • 最后登录2018-12-23
  • 发帖数72
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
4楼#
发布于:2010-01-18 12:22
哈哈,多谢xelnaga

我用过screengrab,它的全页面截屏不能用……怪哉

我把你给我的代码一股脑扔到“MouseGestures.uc.js”(http://www.xuldev.org/misc/ucjs.php)里面了……

居然都好用,哈哈,多谢多谢

只是划手势的时候以前状态栏里的中文提示“鼠标手势:……”变成了英文的“Gesture:……”

我自己到脚本里找这个单词,改成中文(用EditPlus修改),出来的是乱码……汗

请问大侠,我直接粘贴到MouseGestures.uc.js里面是否有多余的代码呢?要删掉点什么吗?

多谢~~~
xelnaga
千年狐狸
千年狐狸
  • UID1911
  • 注册日期2005-01-08
  • 最后登录2022-04-20
  • 发帖数1725
  • 经验85枚
  • 威望0点
  • 贡献值8点
  • 好评度5点
  • 社区居民
5楼#
发布于:2010-01-18 12:22
存UTF-8看看

不加修改应该会有多余代码
主要是注意到底哪个function函数是执行你需要的功能的,留意一下括号的封闭,还有一些是往菜单里加东西的,就明显不需要了
agate
小狐狸
小狐狸
  • UID327
  • 注册日期2004-11-29
  • 最后登录2018-12-23
  • 发帖数72
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
6楼#
发布于:2010-01-18 12:22
多谢xelnaga大侠

在这里已经解决了,呵呵
http://board.mozest.com/thread-33146-1-1

多谢
just4fun
千年狐狸
千年狐狸
  • UID30408
  • 注册日期2009-09-17
  • 最后登录2016-04-28
  • 发帖数1497
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度2点
7楼#
发布于:2010-01-18 12:22
我还是WebScreenShot,甚少截屏,但是看重它可以把网页中的图片转化为base64
游客

返回顶部