Created
June 14, 2013 18:39
-
-
Save mallowlabs/5784215 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
// ==UserScript== | |
// @name googledefine_popup.user.js | |
// @author mallowlabs | |
// @version 0.0.2 | |
// @namespace http://mallowlabs.s206.xrea.com/ | |
// @published 2006-12-01 | |
// @modified 2006-12-07 | |
// @description : Popup "Google Define" by word selection | |
// @include http://* | |
// | |
// @license GPL | |
// Copyright (C) 2005, hatena. | |
// Released under the GPL license | |
// http://www.gnu.org/copyleft/gpl.html | |
// | |
// id:takef | |
// HatenaDiaryKeywordPopupForOpera.user.js | |
// http://userjs.seesaa.net/article/13853826.html | |
// | |
// ==/UserScript== | |
(function () { | |
var xpos = 0; | |
var ypos = 0; | |
var scrollX = 0; | |
var scrollY = 0; | |
function createPopup() { | |
var popup = document.createElement('div'); | |
with(popup.style) { | |
visibility = 'hidden'; | |
fontSize = '10pt'; | |
fontFamily = 'sans-serif'; | |
textAlign = 'left'; | |
lineHeight = '110%'; | |
color = '#333333'; | |
paddingLeft = '5px'; | |
paddingRight = '5px'; | |
backgroundColor = 'cornsilk'; | |
border = '1px solid #333333'; | |
position = 'absolute'; | |
left = (xpos + 10) + 'px' ; | |
top = (ypos + 25) + 'px'; | |
width = '250px'; | |
} | |
popup.id = 'gdpopup'; | |
return popup; | |
} | |
function hidePopup() { | |
var popup = document.getElementById('gdpopup'); | |
document.body.removeChild(popup); | |
document.removeEventListener("click", hidePopup, false); | |
} | |
function appendPopup(t) { | |
var popup = document.getElementById('gdpopup'); | |
if (popup) { | |
hidePopup(); | |
} | |
var popupDiv = createPopup(); | |
popupDiv.innerHTML = t; | |
popup = document.body.appendChild(popupDiv); | |
var r = popup.offsetLeft + popup.offsetWidth; | |
if (r > scrollX + document.body.clientWidth) { | |
popup.style.left = (scrollX + document.body.clientWidth - popup.offsetWidth - 10) + 'px'; | |
} | |
var b = popup.offsetTop + popup.offsetHeight; | |
if (b > scrollY + document.body.clientHeight) { | |
var t = scrollY + document.body.clientHeight - popup.offsetHeight - 10; | |
if (t < scrollY) | |
t = scrollY + 10; | |
popup.style.top = t + 'px'; | |
} | |
document.addEventListener('click', hidePopup, false); | |
popup.style.visibility = 'visible'; | |
} | |
function showPopup(e, t) { | |
setPos(e); | |
GM_xmlhttpRequest({ | |
method:"GET", | |
url:"http://www.google.co.jp/search?q=define%3A" + encodeURI(t), | |
onload:function(details) { | |
var res = details.responseText.match(/<li>.+?</i); | |
res = res + ""; | |
if (res.indexOf("<")==-1) { | |
res = "Not Found"; | |
} else { | |
res = res.substring(4, res.length-1); | |
} | |
appendPopup(res); | |
} | |
}); | |
} | |
function setPos(e) { | |
xpos = e.pageX; | |
ypos = e.pageY; | |
scrollX = e.pageX - e.clientX; | |
scrollY = e.pageY - e.clientY; | |
} | |
function trim(argValue){ | |
return String(argValue).replace(/^ */gim, "").replace(/ *$/gim, ""); | |
} | |
document.addEventListener('mouseup', function(e){ | |
var t = window.getSelection(); | |
t = t + ""; | |
if(t.match(/^ *[a-zA-Z]+ *$/)) { | |
t = trim(t); | |
showPopup(e, t); | |
} | |
}, false); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment