Skip to content

Instantly share code, notes, and snippets.

@negipo
Created July 1, 2009 15:01
Show Gist options
  • Save negipo/138824 to your computer and use it in GitHub Desktop.
Save negipo/138824 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name FindArchivedContent
// @namespace http://polog.org/
// @include http*://*
// @require http://gist.github.com/3242.txt
// ==/UserScript==
// using $X http://gist.github.com/3242 by os0x
GM_addStyle('div.gm_po_icon{position: fixed; top: 3px; color: #CCC;width: 10px; height:10px;z-index: 255;}' +
'div.gm_po_content{position: fixed; top: -300px; color: black;width: 120px; height: 200px; overflow-y: scroll;border: 1px solid black; background-color: white;font-size: 10px; color: #000;text-align:left;font-weight:normal;line-height:120%;font-family:verdana; z-index: 256; padding: 0px;}' +
'div.gm_po_content ul{margin: 0px; padding: 0px;}' +
'div.gm_po_content li{list-style-image:none;list-style-position:outside;list-style-type:none;}' +
'div.gm_po_content a{color:black;}'
);
const right_margin = '16px';
const COLOR = {
init: '#FA0',
loading: '#A0F',
caution: '#A00'
};
var FindArchived = function(){
this.requestURL = 'http://web.archive.org/web/*/' + location.href;
this.init_icon();
}
FindArchived.prototype = {
hideContent: function(){
this.content.style.top = '-200px';
},
search: function(){
var self = this;
this.icon.style.backgroundColor = COLOR.loading;
var opt = {
method: 'get',
url: this.requestURL,
onload: function(res){
self.requestLoad.apply(self, [res]);
}
}
GM_xmlhttpRequest(opt);
},
requestLoad: function(res){
this.content.innerHTML = '';
var self = this;
var t = res.responseText;
var htmlDoc = createHTMLDocumentByString(t);
if(!/Search Result/.test($X('descendant::table[last()]', htmlDoc)[0].innerHTML)){
this.icon.style.backgroundColor = COLOR.caution;
return;
}
var ul = document.createElement('ul');
$X('descendant::table[last()]/descendant::a', htmlDoc).forEach(function(e){
var li = document.createElement('li');
li.appendChild(e);
ul.appendChild(li);
});
this.content.appendChild(ul);
this.content.style.top = '3px';
this.icon.style.backgroundColor = COLOR.init;
},
init_icon: function(){
var self = this;
this.icon = document.createElement('div');
this.icon.className = 'gm_po_icon';
this.icon.style.right = right_margin;
this.icon.style.backgroundColor = COLOR.init;
this.content = document.createElement('div');
this.content.className = 'gm_po_content';
this.content.style.right = right_margin;
document.body.appendChild(this.icon);
document.body.appendChild(this.content);
this.icon.addEventListener('mouseover', function(){self.search.apply(self)}, true);
//this.content.addEventListener('mouseout', function(){self.hideContent.apply(self)}, true);
}
}
new FindArchived();
// from LDR - Signal
function update(obj, params){
if(obj.setAttribute){
for(var key in params)
obj.setAttribute(key, params[key]);
} else {
for(var key in params)
obj[key] = params[key];
}
return obj;
}
// from Autopagerize
function createHTMLDocumentByString(str) {
if (document.documentElement.nodeName != 'HTML') {
return new DOMParser().parseFromString(str, 'application/xhtml+xml')
}
var html = strip_html_tag(str)
var htmlDoc = document.implementation.createDocument(null, 'html', null)
var fragment = createDocumentFragmentByString(html)
try {
fragment = htmlDoc.adoptNode(fragment)
} catch(e) {
fragment = htmlDoc.importNode(fragment, true)
}
htmlDoc.documentElement.appendChild(fragment)
return htmlDoc
}
function strip_html_tag(str) {
var re = /^[\s\S]*?<html(?:[ \t\r\n][^>]*)?>|<\/html[ \t\r\n]*>[\w\W]*$/ig
return str.replace(re, '')
}
function createDocumentFragmentByString(str) {
var range = document.createRange()
range.setStartAfter(document.body)
return range.createContextualFragment(str)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment