引用符をつけてコピーするuserChrome.uc.js CopyRef

引用符ってのは例えば

ハロー、
ワールド

こんな文章をコピーしたとき

>ハロー、
>ワールド

こんな風にしてくれる機能
いままではCopyURL改造して使ってたんだけどfxのバージョンアップの度に改造するのが面倒になったのでuserChrome.js使うようにしてみた。

あたりを参考にした。
土台はcopy-url-liteで元を生かすように改造した(と言うかほとんどパクった)ので共存するように改造するのもたやすいはず。
はてなダイアリはテキスト添付できないのか。面倒だな)

空白の引用符ってなにに使うの?といわれそうw
pukiwikiのpre引用がこの形だったのです。

copyref.uc.js

// ==UserScript==
// @name           Copy Ref
// @version        0.0.0
// @include        chrome://browser/content/browser.xul
// @note           http://www.code-404.net/article/2007/07/15/copy-url-lite を元に改造
// ==/UserScript==

(function(){
  
  var locale = Components.classes["@mozilla.org/preferences-service;1"].
    getService(Components.interfaces.nsIPrefBranch);
  locale = locale.getCharPref("general.useragent.locale");
  
  var mMenus = [
    {
      //引用符つきコピー(>)
      label: ">xxx",
      text: '',
      condition: "select",
      ref: '>'
    },
    {
      //引用符つきコピー(空白)
      label: "\u7A7A\u767D\u000A",
      text: '',
      condition: "select",
      ref: ' '
    },
    {
      //引用符つきコピー(> & 40文字)
      label: ">xxx40",
      text: '',
      condition: "select",
      ref: '>',
      line: 40
    },
  ];
  
  init: {
    var contextMenu = document.getElementById("contentAreaContextMenu");
    var separator = document.getElementById("context-sep-properties");
    
    var menu = document.createElement("menu");
    menu.id = "copyurllite";
    menu.setAttribute("label", "Copy Ref");
    menu.className = "menu-iconic";

    contextMenu.insertBefore(menu, separator);
    
    var menuPopup = document.createElement("menupopup");
    menu.appendChild(menuPopup);
    
    for(var i = 0, menu; menu = mMenus[i]; i++){
      var menuItem;
      if(menu.label == "separator"){
        menuItem = document.createElement("menuseparator");
      }
      else{
        menuItem = document.createElement("menuitem");
        menuItem.setAttribute("label", menu.label);
        if("accesskey" in menu) menuItem.setAttribute("accesskey", menu.accesskey);
        menuItem.culMenu = menu;
        menuItem.addEventListener("command", copyText, false);
      }
      menuItem.id = "copyurllite-menu-" + i;
      menuPopup.appendChild(menuItem);
    }
  
    contextMenu.addEventListener("popupshowing", setMenuDisplay, false);
  }
  
  function copyText(aEvent){
    
    function htmlEscape(text) {
      text = text.replace(/&/g, "&");
      text = text.replace(/>/g, ">");
      text = text.replace(/</g, "&lt;");
      text = text.replace(/"/g, "&quot;");
      return text;
    }
    
    var text = aEvent.target.culMenu.text;
    var win = content.document;
    var title = win.title;
    var title_html = htmlEscape(title);
    var url = win.location.href;
    var url_html = htmlEscape(url);
    if(gContextMenu.isTextSelected){
      //var sel = content.getSelection().toString();
      var sel = getSelectString();
      var sel_html = htmlEscape(sel);
    }
    if(gContextMenu.onLink){
      var link = gContextMenu.getLinkURL().toString();
      var link_html = htmlEscape(link);
    }
    if(gContextMenu.onImage){
      var imageUri = gContextMenu.imageURL;
      var imageUriHtml = htmlEscape(imageUri);
      var imageAlt = gContextMenu.target.alt;
      var imageAltHtml = htmlEscape(imageAlt);
      var imageTitle = gContextMenu.target.title;
      var imageTitleHtml = htmlEscape(imageTitle);
    }
    //var eol = "\r\n";
    var eol = eol();
    
    /**
     * Returns the end of line char based on platform
     * Code taken from: Copy URL+
     */
    function eol() {
    	platform = navigator.platform.toLowerCase(); 
    	if (platform.indexOf('win') != -1) 
        return "\r\n"; // the bor(g)ing platform
    	else if (platform.indexOf('mac') != -1) 
        return "\r"; // apple
    	else if (platform.indexOf('unix') != -1 
        	|| platform.indexOf('linux') != -1 
        	|| platform.indexOf('sun') != -1) 
        return "\n"; // good boys
    }
    
    function copyRef_addRef(str, ref, rmLN, line){
      str = str.replace("\r\n","\n");
      str = str.replace("\r","\n");
     	if(rmLN){
	    	str = str.replace("\n", "");
    	}
    	var strAry = str.split("\n");
    	var rstAry = new Array();
    	for(i in strAry){
        var s = strAry[i];
       	while(line && line < s.length){
          rstAry.push( s.slice(0, line) );
          s = s.slice(line);
      	}
      	rstAry.push( s );
    	}
    	return ref + rstAry.join(eol + ref);
    }

    //preタグだとselの段階で改行が消されてる対策もしたい
    //軽量マウスジェスチャー用をどーぞ,より拝借
    function getSelectString(){
    	function getWindow(){
    	var focusedWindow = document.commandDispatcher.focusedWindow;
    	if (!focusedWindow || focusedWindow == window) return window.content;
    	else return focusedWindow;
    	}
    	var targetWindow = getWindow();
    	var sel = Components.lookupMethod(targetWindow, 'getSelection').call(targetWindow);
    	//インプット/テキストエリアもね
    	if (sel && !sel.toString()) {
    	var node = document.commandDispatcher.focusedElement;
    	if (node && (node.type == "text" || node.type == "textarea") && 'selectionStart' in node && node.selectionStart != node.selectionEnd) {
    	var offsetStart = Math.min(node.selectionStart, node.selectionEnd);
    	var offsetEnd = Math.max(node.selectionStart, node.selectionEnd);
    	return node.value.substr(offsetStart, offsetEnd-offsetStart);
    	}
    	}
    	return sel ? sel.toString() : "";
    }

    if("ref" in aEvent.target.culMenu){
      var ref = aEvent.target.culMenu.ref;
      var line = aEvent.target.culMenu.line;
      Cc["@mozilla.org/widget/clipboardhelper;1"]
        .getService(Ci.nsIClipboardHelper).copyString(copyRef_addRef(sel,ref,false,line));
    }else{
      /*Cc["@mozilla.org/widget/clipboardhelper;1"]
        .getService(Ci.nsIClipboardHelper).copyString(convertText(text));*/
    }
  }
  
  function setMenuDisplay(){
    for (var i = 0, menu; menu = mMenus[i]; i++)
      document.getElementById("copyurllite-menu-" + i).hidden =
        menu.condition == "select" ?
          !gContextMenu.isTextSelected :
        menu.condition == "link" ?
          !gContextMenu.onLink :
        menu.condition == "image" ?
          !gContextMenu.onImage :
        menu.condition == "select-link" ?
          !(gContextMenu.isTextSelected && gContextMenu.onLink) :
        menu.condition == "select-image" ?
          !(gContextMenu.isTextSelected && gContextMenu.onImage) :
        menu.condition == "link-image" ?
          !(gContextMenu.onLink && gContextMenu.onImage) :
        menu.condition == "select-link-image" ?
          !(gContextMenu.isTextSelected && gContextMenu.onLink && gContextMenu.onImage) : false;
  }
  
})();