Skip to content

Instantly share code, notes, and snippets.

@fuba
Created May 8, 2009 22:15
Show Gist options
  • Save fuba/109035 to your computer and use it in GitHub Desktop.
Save fuba/109035 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Courteous favorite on dashboard
// @namespace http://fuba.moarningnerds.org/
// @description favorite a reblogged item using their original service
// @include http://www.tumblr.com/dashboard*
// @require http://gist.github.com/3238.txt
// ==/UserScript==
var SITEINFO = [
{
name: 'pixiv',
xpath: './/div[@class="caption"]//a[contains(@href, "www.pixiv.net")]',
regexp: '^.*illust_id\=(\\d+)$',
replace: 'http://www.pixiv.net/bookmark_add.php?type=illust&illust_id=[1]',
icon: 'http://www.pixiv.net/favicon.ico',
}
];
var filter = function (root) {
var photos = $X('.//li[contains(@class, "post photo")]', root);
if (photos.length == 0 && root.className.match(/post photo/)) {
photos.push(root);
}
photos.forEach(function (li) {
var postControls = $X(".//div[@class='post_controls']", li)[0];
SITEINFO.forEach(function(pat){
var matches = $X(pat.xpath, li);
if (matches[0] && matches[0].href) {
var url = matches[0].href;
var re = new RegExp(pat.regexp, 'i');
var refs = url.match(re);
if (refs) {
var template = pat.replace;
refs.forEach(function(ref){
if ('undefined' != typeof(ref)) {
var numre = new RegExp('\\[\\d+\\]');
template = template.replace(numre, ref);
}
});
postControls.appendChild(createFavIcon({
url: template,
service: pat
}));
}
}
});
});
};
var createFavIcon = function (item) {
var favanc = document.createElement('A');
favanc.addEventListener('mouseup', function () {
openIframe(item.url)
}, false);
if (item.service.icon) {
var favicon = document.createElement('IMG');
favicon.src = item.service.icon;
favicon.alt = item.service.name;
favicon.title = item.service.name;
favanc.appendChild(favicon);
}
else {
favanc.appendChild(document.createTextNode(item.service.name));
}
return favanc;
};
var openIframe = function (url) {
var body = document.body;
var iframe = document.createElement('iframe');
iframe.width = parseInt(window.innerWidth * 0.7);
iframe.height = window.innerHeight;
iframe.src = url;
iframe.className = 'floatingiframe';
iframe.id = 'floatingiframe';
body.appendChild(iframe);
var iframekiller = document.createElement('div');
iframekiller.className = 'iframekiller';
iframekiller.addEventListener('mouseup', function () {
body.removeChild(iframe);
body.removeChild(this);
}, false);
body.appendChild(iframekiller);
};
GM_addStyle(['.floatingiframe {position:fixed; right:0; top:0; z-index:9999999;}']);
GM_addStyle(['.iframekiller {position:fixed; left:0; top:0; width:100%; height:100%; z-index:100; opacity: 1}']);
filter(document.body);
if (window.AutoPagerize)
window.AutoPagerize.addFilter(function(docs){docs.forEach(filter);});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment