Last active
August 29, 2015 14:07
-
-
Save whiteleaf7/99b03efd33f32f2063f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function AmazonLinker() { | |
this.initialize(); | |
} | |
AmazonLinker.prototype = { | |
amazon_url_regexp: /(http:\/\/www\.amazon\.(?:com|co\.jp))\/(.+)/, | |
active_mode_interval: 500, | |
inactive_mode_interval: 10 * 1000, | |
shift_to_inactive_mode_timeout: 10 * 60 * 1000, | |
shift_to_inactive_mode_timer_id: null, | |
initialize: function() { | |
this.interval = this.inactive_mode_interval; | |
this.before_text = null; | |
this.register_watch_handler(); | |
}, | |
watch_clipboard: function() { | |
var text = clipboard.text; | |
if (text != this.before_text) { | |
this.short_amazon_link(text); | |
this.before_text = text; | |
} | |
this.register_watch_handler(); | |
}, | |
register_watch_handler: function() { | |
var self = this; | |
setTimeout(function() { | |
self.watch_clipboard(); | |
}, this.interval); | |
}, | |
short_amazon_link: function(text) { | |
if (!text.match(this.amazon_url_regexp)) return; | |
var matches = [RegExp.$1]; | |
var elements = RegExp.$2.split("/"); | |
for (var i = 0; i < elements.length; i++) { | |
if (elements[i].match(/^\w+$/)) { | |
matches.push(elements[i]); | |
} | |
} | |
// without search query | |
if (matches[1] != "s") { | |
clipboard.text = matches.join("/"); | |
} | |
}, | |
active_owner: function() { | |
var self = this; | |
this.interval = this.active_mode_interval; | |
this.register_watch_handler(); | |
if (this.shift_to_inactive_mode_timer_id) { | |
clearTimeout(this.shift_to_inactive_mode_timer_id); | |
} | |
this.shift_to_inactive_mode_timer_id = setTimeout(function() { | |
self.interval = self.inactive_mode_interval; | |
}, this.shift_to_inactive_mode_timeout); | |
} | |
}; | |
var amazon_linker; | |
function event::onLoad() { | |
amazon_linker = new AmazonLinker(); | |
} | |
function event::onChannelText(prefix, channel, text) { | |
if (prefix.nick == myNick) { | |
amazon_linker.active_owner(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment