From 7cc889b67fcec3d4d7343a4ad76fbb1685e8ad44 Mon Sep 17 00:00:00 2001 From: suVene Date: Thu, 11 Dec 2008 16:04:02 +0000 Subject: Autopagerize の XPath から ]] をマップする git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@26425 d0d07461-0603-4401-acd4-de1884942a52 --- nextlink.js | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100755 nextlink.js (limited to 'nextlink.js') diff --git a/nextlink.js b/nextlink.js new file mode 100755 index 0000000..842baaa --- /dev/null +++ b/nextlink.js @@ -0,0 +1,150 @@ +// PLUGIN_INFO//{{{ +var PLUGIN_INFO = + + nextlink + mapping "[[", "]]" by Autopagerize XPath. + Autopagerize 用の XPath より "[[", "]]" をマッピングします。 + suVene + 0.1.0 + 2.0 + 2.0pre + +; +//}}} +liberator.plugins.nextlink = (function() { + if (!liberator.plugins.libly) { + liberator.log('nextlink: needs _libly.js'); + return; + } + + var libly = liberator.plugins.libly; + var $U = libly.$U; + var logger = $U.getLogger('nextlink'); + + var NextLink = function() {//{{{ + this.initialize.apply(this, arguments); + }; + NextLink.prototype = { + initialize: function() { + + this.WEDATA_AUTOPAGERIZE = 'http://wedata.net/databases/Autopagerize/items.json'; + this.initialized = false; + this.isCurOriginalMap = true; + this.siteinfo = []; + this.cache = {}; // {url: {xpath: xpath, next: element, prev: url}} or null + + var req = new libly.Request(this.WEDATA_AUTOPAGERIZE); + req.addEventListener('onSuccess', $U.bind(this, + function(res) { + var json = $U.evalJson(res.responseText); + if (!json) return; + this.siteinfo = json.map(function(item) [item.data.url, item.data.nextLink]) + .sort(function(a, b) b[0].length - a[0].length); // sort url.length desc + this.initialized = true; + } + )); + req.get(); + + commands.addUserCommand(['nextlink'], 'map ]] by Autopegrize XPath.', + $U.bind(this, function(args) { this.handler(args); }), null, true + ); + var loadEvent = autocommands["DOMLoad"] || "PageLoad"; // for 1.2 + liberator.execute(':autocmd! ' + loadEvent + ' .* :nextlink onLoad'); + liberator.execute(':autocmd! LocationChange .* :nextlink onLocationChange'); + }, + handler: function(args) { + event = args.string || args; + this[event](buffer.URL); + commandline.echo(''); + }, + onLoad: function(url) { + + if (!this.initialized) return; + if (this.cache[url] && + this.cache[url].hasOwnProperty('xpath')) { + this.onLocationChange(url); + return; + } + + for (let i = 0, len = this.siteinfo.length; i < len; i++) { + if (url.match(this.siteinfo[i][0])) { + this.setCache(url, 'xpath', this.siteinfo[i][1]); + this.onLocationChange(url); + return; + } + } + this.setCache(url, ['xpath', 'next', 'prev'], [null, null, null]); + }, + onLocationChange: function(url) { + + if (!this.initialized || + !this.cache[url] || + !this.cache[url].hasOwnProperty('xpath')) return; + + if (this.cache[url]['xpath'] == null) { + this.restorOrginalMap(); + return; + } + if (this.cache[url].next) this.cache[url].next.style.backgroundColor = '#F00'; + + var elem; + //var matches = buffer.evaluateXPath(this.cache[url]); + //for each (let match in matches) elem = match; + $U.getNodesFromXPath(this.cache[url].xpath, window.content.document, + function(item) elem = item, this); + + var nextUrl = $U.pathToURL(elem, window.content.document); + this.setCache(url, 'next', elem); + this.setCache(nextUrl, 'prev', url); + this.customizeMap(elem, url, this.cache[url].prev, (nextUrl || null)); + }, + customizeMap: function(elem, url, prevUrl, nextUrl) { + + if (!prevUrl) { + mappings.remove(config.browserModes, "[["); + } else { + mappings.addUserMap(config.browserModes, ["[["], "customize by nextlink.js", + function(count) { liberator.open(prevUrl, liberator.CURRENT_TAB); }, + { flags: Mappings.flags.COUNT }); + } + + if (!nextUrl) { + mappings.remove(config.browserModes, "]]"); + } else { + mappings.addUserMap(config.browserModes, ["]]"], "customize by nextlink.js", + function(count) { buffer.followLink(elem, liberator.CURRENT_TAB); }, + { flags: Mappings.flags.COUNT }); + } + + this.isCurOriginalMap = false; + }, + restorOrginalMap: function() { + + if (this.isCurOriginalMap) return; + mappings.remove(config.browserModes, "[["); + mappings.remove(config.browserModes, "]]"); + this.isCurOriginalMap = true; + }, + setCache: function(key, subKeys, values) { + + if (!this.cache[key]) this.cache[key] = {}; + subKeys = [].concat(subKeys); + values = [].concat(values); + for (let i = 0, len = subKeys.length; i < len; i++) this.cache[key][subKeys[i]] = values[i]; + } + }//}}} + + var instance = new NextLink(); + return instance; + +})(); +// vim: set fdm=marker sw=4 ts=4 sts=0 et: + -- cgit v1.2.3