Merge pull request #360 from DakuTree/master

bookmarklet.js support for danbooru2 + fixed most issues with other sites
This commit is contained in:
Shish 2013-12-30 02:16:35 -08:00
commit bc2100940d

View file

@ -1,6 +1,5 @@
/* Imageboard to Shimmie */
// This should work with "most" sites running Danbooru/Gelbooru/Shimmie
// TODO: Make this use jQuery! (if we can be sure that jquery is loaded)
// maxsize, supext, CA are set inside the bookmarklet (see theme.php)
var maxsize = (maxsize.match("(?:\.*[0-9])")) * 1024; // This assumes we are only working with MB.
@ -25,67 +24,84 @@ else if(CA === 2) { // New Tags
}
/*
* Danbooru (oreno.imouto | konachan | sankakucomplex)
* Danbooru2
* jQuery should always active here, meaning we can use jQuery in this part of the bookmarklet.
*/
if(document.getElementById("post_tags") !== null) {
if(document.getElementById("post_tag_string") !== null) {
if (typeof tag !== "ftp://ftp." && chk !==1) {
var tag = document.getElementById("post_tags").value;
var tag = $('#post_tag_string').text().replace(/\n/g, "");
}
tag = tag.replace(/\+/g, "%2B"); // This should stop + not showing in tags :x
tag = tag.replace(/\+/g, "%2B");
var source = "http://" + document.location.hostname + document.location.href.match("\/post\/show\/[0-9]+");
if(source.search("oreno\\.imouto") >= 0 || source.search("konachan\\.com") >= 0) {
var rating = document.getElementById("stats").innerHTML.match("<li>Rating: (.*) <span")[1];
}
else {
var rating = document.getElementById("stats").innerHTML.match("<li>Rating: (.*)<\/li>")[1];
var source = "http://" + document.location.hostname + document.location.href.match("\/posts\/[0-9]+");
var rlist = $('[name="post[rating]"]');
for(x=0;x<3;x++){
var rating = (rlist[x].checked == true ? rlist[x].value : rating);
}
if(tag.search(/\bflash\b/)===-1){
var highres_url = document.getElementById("highres").href;
if(source.search("oreno\\.imouto") >= 0 || source.search("konachan\\.com") >= 0){ // oreno's theme seems to have moved the filesize
var filesize = document.getElementById("highres").innerHTML.match("[a-zA-Z0-9]+ \\(+([0-9]+\\.[0-9]+) ([a-zA-Z]+)");
var fileinfo = $('#sidebar > section:eq(3) > ul > :contains("Size") > a');
var furl = "http://" + document.location.hostname + fileinfo.attr('href');
var fs = fileinfo.text().split(" ");
var filesize = (fs[1] == "MB" ? fs[0] * 1024 : fs[0]);
if(supext.search(furl.match("[a-zA-Z0-9]+$")[0]) !== -1){
if(filesize <= maxsize){
location.href = ste+furl+"&tags="+tag+"&rating="+rating+"&source="+source;
}
else{
alert(toobig);
}
}
else{
alert(notsup);
}
}
/*
* konachan | sankakucomplex | gelbooru
*/
else if(document.getElementById('tag-sidebar') !== null) {
if (typeof tag !== "ftp://ftp." && chk !==1) {
if(document.location.href.search("sankakucomplex\\.com") >= 0 || document.location.href.search("gelbooru\\.com")){
var tag = document.getElementById('tag-sidebar').innerText.replace(/ /g, "_").replace(/[\?_]*(.*?)_(\(\?\)_)?[0-9]+\n/g, "$1 ");
}else{
var filesize = document.getElementById("stats").innerHTML.match("[0-9] \\(((?:\.*[0-9])) ([a-zA-Z]+)");
}
if(filesize[2] == "MB") {
var filesize = filesize[1] * 1024;
}
else {
var filesize = filesize[2].match("[0-9]+");
}
if(supext.search(highres_url.match("http\:\/\/.*\\.([a-z0-9]+)")[1]) !== -1) {
if(filesize <= maxsize) {
if(source.search("oreno\\.imouto") >= 0) {
// this regex tends to be a bit picky with tags -_-;;
var highres_url = highres_url.match("(http\:\/\/[a-z0-9]+\.[a-z]+\.[a-z]\/[a-z0-9]+\/[a-z0-9]+)\/[a-z0-9A-Z%_-]+(\.[a-zA-Z0-9]+)");
var highres_url = highres_url[1]+highres_url[2]; // this should bypass hotlink protection
}
else if(source.search("konachan\\.com") >= 0) {
// konachan affixs konachan.com to the start of the tags, this requires different regex
var highres_url = highres_url.match("(http\:\/\/[a-z0-9]+\.[a-z]+\.[a-z]\/[a-z0-9]+\/[a-z0-9]+)\/[a-z0-9A-Z%_]+\.[a-zA-Z0-9%_-]+(\.[a-z0-9A-Z]+)")
var highres_url = highres_url[1]+highres_url[2];
}
location.href = ste+highres_url+"&tags="+tag+"&rating="+rating+"&source="+source;
}
else{
alert(toobig);
}
}
else{
alert(notsup);
var tag = document.getElementById("post_tags").value;
}
}
else {
if(supext.search("swf") !== -1) {
location.href = ste+document.getElementsByName("movie")[0].value+"&tags="+tag+"&rating="+rating+"&source="+source;
tag = tag.replace(/\+/g, "%2B");
var source = "http://" + document.location.hostname + (document.location.href.match("\/post\/show\/[0-9]+") || encodeURIComponent(document.location.href.match(/\/index\.php\?page=post&s=view&id=[0-9]+/)));
var rating = document.getElementById("stats").innerHTML.match("Rating: ([a-zA-Z]+)")[1];
if(source.search("sankakucomplex\\.com") >= 0 || source.search("konachan\\.com") >= 0){
var fileinfo = document.getElementById("highres");
//NOTE: If highres doesn't exist, post must be flash (only sankakucomplex has flash)
}else if(source.search("gelbooru\\.com") >= 0){
var fileinfo = document.getElementById('pfd').parentNode.parentNode.getElementsByTagName('a')[0];
//gelbooru has no easy way to select the original image link, so we need to double check it is the correct link.
fileinfo = (fileinfo.getAttribute('href') == "#" ? document.getElementById('pfd').parentNode.parentNode.getElementsByTagName('a')[1] : fileinfo);
}
fileinfo = fileinfo || document.getElementsByTagName('embed')[0]; //If fileinfo is null then image is most likely flash.
var furl = fileinfo.href || fileinfo.src;
var fs = (fileinfo.innerText.match(/[0-9]+ (KB|MB)/) || ["0 KB"])[0].split(" ");
var filesize = (fs[1] == "MB" ? fs[0] * 1024 : fs[0]);
if(supext.search(furl.match("[a-zA-Z0-9]+$")[0]) !== -1){
if(filesize <= maxsize){
location.href = ste+furl+"&tags="+tag+"&rating="+rating+"&source="+source;
}
else{
alert(notsup);
alert(toobig);
}
}
else{
alert(notsup);
}
}
/*
@ -128,37 +144,4 @@ else if(document.getElementsByTagName("title")[0].innerHTML.search("Image [0-9.-
alert(notsup);
}
}
}
/*
* Gelbooru
*/
else if(document.getElementById("tags") !== null) {
if (typeof tag !=="ftp://ftp." && chk !==1) {
var tag = document.getElementById("tags").value;
}
var rating = document.getElementById("stats").innerHTML.match("<li>Rating: (.*)<\/li>")[1];
// Can't seem to grab source due to url containing a &
// var source="http://" + document.location.hostname + document.location.href.match("\/index\.php?page=post&amp;s=view\\&amp;id=.*");
// Updated Nov. 24, 2013 by jgen.
var gmi;
try {
gmi = document.getElementById("image").src.match(".*img[0-9]*\.gelbooru\.com[\/]+images[\/]+[0-9]+[\/]+[a-z0-9]+\.[a-z0-9]+")[0];
// Since Gelbooru does not allow flash, no need to search for flash tag.
// Gelbooru doesn't show file size in statistics either...
if(supext.search(gmi.match("http\:\/\/.*\\.([a-z0-9]+)")[1]) !== -1){
location.href = ste+gmi+"&tags="+tag+"&rating="+rating;//+"&source="+source;
}
else{
alert(notsup);
}
}
catch (err)
{
alert("Unable to locate the image on the page!\n(Gelbooru may have changed the structure of their page, please file a bug.)");
}
}
}