From 508c26710cec1db4f99cc19827abdb1c393851aa Mon Sep 17 00:00:00 2001 From: secondlife Date: Tue, 17 Feb 2009 10:12:08 +0000 Subject: 拡張ヒントモード・コピペプラグイン git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@30188 d0d07461-0603-4401-acd4-de1884942a52 --- hints-yank-paste.js | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 hints-yank-paste.js (limited to 'hints-yank-paste.js') diff --git a/hints-yank-paste.js b/hints-yank-paste.js new file mode 100644 index 0000000..a984367 --- /dev/null +++ b/hints-yank-paste.js @@ -0,0 +1,111 @@ +var PLUGIN_INFO = + + {NAME} + add "Yank element's text/html/attrs" or "Paste to element" hint mode + 2.0 + 2.0 + http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/hints-copy.js + Yuichi Tateno + MPL 1.1/GPL 2.0/LGPL 2.1 + 0.1 + +; + +(function() { +var p = function(msg) { + liberator.log(msg, 0); +}; + +const DEFAULT_MAPS = ['c', 'C', 'p', 'P']; +const DEFAULT_YANK_HINTTAGS = 'h1 h2 h3 h4 h5 h6 pre p ul ol blockquote img code input textarea'. + split(/\s+/).map(function(t) '//' + t).join(' | '); +const TEXT_ATTRS = 'src value href title alt'.split(/\s+/); + +const DEFAULT_PASTE_HINTTAGS = '//input[@type="text" or @type="password" or @type="search" or not(@type)] | //textarea'; + +options.add(["hintyanktags"], + "XPath string of hintable elements activated by 'hints-yank'", + "string", DEFAULT_YANK_HINTTAGS); + +options.add(["hintpastetags"], + "XPath string of hintable elements activated by 'hints-paste'", + "string", DEFAULT_PASTE_HINTTAGS); + +let maps = liberator.globalVariables.hints_copy_maps || DEFAULT_MAPS; + +var stripText = function(text) { + text = text.replace(/(^\s+\r?\n)|(\s+$)/m, ''); + let matched = text.match(/(\r?\n)/mg); + if (!matched || matched.length == 1) + text = text.replace(/^\s+/, ''); + return text; +} + +if (maps[0]) // c + hints.addMode(maps[0], 'Yank TEXT', function(elem) { + let text = elem.textContent; + if (!text) + for (let i = 0; i < TEXT_ATTRS.length; i++) + if (text = elem[TEXT_ATTRS[i]]) break; + + util.copyToClipboard(stripText(text), true); + }, function() options['hintyanktags']); + +if (maps[1]) // C + hints.addMode(maps[1], 'Yank HTML', function(elem) { + elem = elem.cloneNode(true); + let tmp = window.content.document.createElement('div'); + tmp.appendChild(elem); + util.copyToClipboard(tmp.innerHTML, true); + }, function() options['hintyanktags']); + +var replaceOrAppend = function(replace) { + return function(elem) { + let clipboard = util.readFromClipboard(); + if (clipboard && clipboard.length) { + if (elem.tagName == 'INPUT') + clipboard.replace(/\r?\n/, ' '); + + if (replace) { + elem.value = clipboard; + } else { + elem.value += clipboard; + } + } + } +} + +if (maps[2]) // p + hints.addMode(maps[2], 'paste text (append)', replaceOrAppend(false), function() options['hintpastetags']); + +if (maps[3]) // P + hints.addMode(maps[3], 'paste text (replace)', replaceOrAppend(true), function() options['hintpastetags']); + +})(); + -- cgit v1.2.3