Skip to content

Instantly share code, notes, and snippets.

@tlync
Created February 26, 2011 03:52
Show Gist options
  • Save tlync/844918 to your computer and use it in GitHub Desktop.
Save tlync/844918 to your computer and use it in GitHub Desktop.
Save search history on alc
// ==UserScript==
// @name ALC Search History
// @namespace http://d.hatena.jp/tlync
// @description Save search history on alc
// @homepage http://twitter.com/tlync
// @include http://www.alc.co.jp/
// @include http://eow.alc.co.jp/*
// @version 0.1.0 alpha
// ==/UserScript==
var $ = document.getElementById;
var DEBUG = GM_getValue('debug');
var log = function(msg){
if(typeof DEBUG != 'undefined' && DEBUG){
GM_log(msg);
}
};
function init(){
registerSearchHandlers();
createActionLinks();
}
// ------------------------------
// Event, DOM Manipulations
// ------------------------------
function registerSearchHandlers(){
var field = getFirstElementByXPath('//form[@id="fm1"]/input');
var submit = getFirstElementByXPath('//input[@type="button"]', field);
var results = $('resultsList');
var suggestions = getElementsByXPath('//div[@id="resultsArea"]/div[@class="sas"]//a[@href]');
field.addEventListener('keypress', function(e){
if(e.which == 13){ //return
log('enter was pressed');
appendHistory(field.value);
}
}, true);
submit.addEventListener('click', function(){
log('submit was clicked');
appendHistory(field.value);
}, true);
if(results != null){
results.addEventListener('dblclick', function(e){
log('one of result word was double clicked');
appendHistory(getSelection());
}, true);
}
if(suggestions != null){
for(var i=0; i<suggestions.length; i++){
var a = suggestions[i];
a.addEventListener('click', function(){
log('one of suggetion was clicked');
appendHistory(this.textContent);
}, true);
}
}
}
function createActionLinks(){
var a = document.createElement('a');
a.textContent = '履歴を表示';
a.style.fontSize = '12px';
a.style.cssFloat = 'left';
a.style.color = '#000088';
a.style.cursor = 'pointer';
a.style.textIndent = '14px';
a.style.background = 'transparent url("http://eowimg.alc.co.jp/content/img/arrow_orange2.gif") no-repeat scroll 0 center';
var a2 = a.cloneNode(false);
a2.textContent = '履歴をクリア';
a2.style.marginLeft = '1em';
var parent = getFirstElementByXPath('//div[@id="resultsArea"]/div[@class="partition"]');
parent.appendChild(a);
parent.appendChild(a2);
a.addEventListener('click', function(){
createWordList(GM_getValue('history'));
}, true);
a2.addEventListener('click', function(){
clearHistory();
}, true);
}
function createWordList(history){
if(!history){
$('resultsList').innerHTML = '<p style="color:#999">検索履歴がありません<p>';
return;
}
var h = history.split(/\n/);
var tpl = '<li><a class="remembered" style="border-radius: 3px; border: 1px solid #ccc; color: #6c6c6c; font-size: 11px; padding: 1px 2px; margin-right: 0.8em; cursor: pointer;">覚えた</a><span class="midashi">{}<span></li>';
var html = [];
html.push('<ul>');
for(var i=0; i<h.length; i++){
html.push(tpl.replace('{}', h[i].split(',')[0]));
}
html.push('</ul>');
var resultsList = $('resultsList');
resultsList.innerHTML = html.join('');
var links = resultsList.getElementsByTagName('a');
for(i=0; i<links.length; i++){
links[i].addEventListener('click', function(e){
var w = this.nextSibling.textContent;
removeHistory(w);
this.parentNode.style.display = 'none';
}, true);
links[i].addEventListener('dblclick', function(e){
e.stopPropagation();
}, true);
}
}
// ------------------------------
// History Manipulations
// ------------------------------
function removeHistory(word){
log('removeHistory - ' + word);
if(!word) return;
var history = GM_getValue('history');
var h = history.split(/\n/);
for(var i=0, l=h.length; i<l; i++){
if(h[i].split(',')[0] == word){
l--; h.splice(i--, 1);
}
}
GM_setValue('history', h.join('\n'));
log('removed the word - ' + word);
}
function appendHistory(word){
log('appendHistory - ' + word);
if(!word) return;
var h = GM_getValue('history');
h = !h ? '' : h + '\n';
GM_setValue('history', h + word + ',' + new Date().getTime());
log('added new word - ' + word);
}
function clearHistory(){
if(confirm('今までの検索履歴を削除します。実行しますか?')){
GM_setValue('history', '');
alert('検索履歴を削除しました。');
log('history has been cleared.');
}
}
// ------------------------------
// GM Menu Commands
// ------------------------------
GM_registerMenuCommand('ALC Search History - Clear History', clearHistory);
GM_registerMenuCommand('ALC Search History - Show History', function(){
alert(GM_getValue('history'));
});
GM_registerMenuCommand('ALC Search History - Enable Debugging', function(){
GM_setValue('debug', true);
alert('Debbuging Mode: enabled');
window.location.reload();
});
GM_registerMenuCommand('ALC Search History - Disable Debugging', function(){
GM_setValue('debug', false);
alert('Debbuging Mode: disabled');
window.location.reload();
});
// ------------------------------
// XPath Utilities
// ------------------------------
function getElementsByXPath(xpath, node) {
var snapshots = getXPathResult(xpath, node, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
var data = [];
for(var i=0; i<snapshots.snapshotLength; i++){
data.push(snapshots.snapshotItem(i));
}
return data;
}
function getFirstElementByXPath(xpath, node) {
var result = getXPathResult(xpath, node, XPathResult.FIRST_ORDERED_NODE_TYPE);
return result.singleNodeValue;
}
function getXPathResult(xpath, node, resultType) {
node = node || document;
var doc = node.ownerDocument || node;
var resolver = doc.createNSResolver(node.documentElement || node);
var defaultNS = node.lookupNamespaceURI(null);
if(defaultNS){
const defaultPrefix = '__default__';
xpath = addDefaultPrefix(xpath, defaultPrefix);
var defaultResolver = resolver;
resolver = function (prefix){
return (prefix == defaultPrefix) ?
defaultNS : defaultResolver.lookupNamespaceURI(prefix);
};
}
return doc.evaluate(xpath, node, resolver, resultType, null);
}
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment