Last active
December 4, 2021 00:35
-
-
Save ailispaw/a08ce031622390288980cdea27437108 to your computer and use it in GitHub Desktop.
Fix Tumblr.ReBlog on Dashboard 2021.12
This file contains hidden or 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
// ==Taberareloo== | |
// { | |
// "name" : "Fix Tumblr.ReBlog on Dashboard 2021.12" | |
// , "description" : "Fix Tumblr.ReBlog on Dashboard 2021.12" | |
// , "include" : ["background", "content"] | |
// , "match" : ["*://*/*"] | |
// , "version" : "0.0.8" | |
// , "downloadURL" : "https://gist.githubusercontent.com/ailispaw/a08ce031622390288980cdea27437108/raw/patch.fix.tumblr.reblog-dashboard.tbrl.js" | |
// } | |
// ==/Taberareloo== | |
(function() { | |
if (inContext('background')) { | |
TBRL.setRequestHandler('extractByLink', function (req, sender, func) { | |
var ctx = req.content; | |
var anchor = $N('a', {href: ctx.href}); | |
Tumblr.addBeforeSendHeader(); | |
request(Tumblr.TUMBLR_URL + 'api/v2/blog/' + anchor.host + '/posts/' + ctx.post_id + '/permalink', { | |
responseType : 'json', | |
queryString: { | |
fields : 'name,title,url' | |
}, | |
headers : { | |
Authorization : 'Bearer ' + Tumblr.STATE.apiFetchStore.API_TOKEN | |
} | |
}).then(function (res) { | |
var json = res.response; | |
var id = json.response.timeline.elements[0].id; | |
var reblog_key = json.response.timeline.elements[0].reblog_key | |
request( Tumblr.TUMBLR_URL + 'neue_web/iframe/reblog/' + id + '/' + reblog_key, { | |
responseType: 'document' | |
}).then( function ( res ) { | |
Tumblr.removeBeforeSendHeader(); | |
var doc = res.response; | |
Tumblr.form_key = $X('//meta[@id="tumblr_form_key"]/@content', doc)[0]; | |
func({ | |
reblog_id : id, | |
reblog_key : reblog_key | |
}); | |
}).catch( function ( err ) { | |
Tumblr.removeBeforeSendHeader(); | |
func({ | |
err : err | |
}); | |
}); | |
}).catch(function (err) { | |
Tumblr.removeBeforeSendHeader(); | |
func({ | |
err : err | |
}); | |
}); | |
}); | |
return; | |
} | |
if (inContext('content')) { | |
var cssMap = {}; | |
var postClass = ""; | |
var script = $X("//body/script[@nonce]")[0].text; | |
var validJSON = script.match(/window\['___INITIAL_STATE___'\] ?= ?(.*?);\n/)[1]; | |
validJSON = validJSON.replace(/:undefined,/g, ':null,'); | |
validJSON = validJSON.replace(/:undefined]/g, ':null]'); | |
validJSON = validJSON.replace(/:undefined}/g, ':null}'); | |
var STATE = JSON.parse(validJSON); | |
console.log(STATE); | |
if (STATE && STATE.isLoggedIn.isLoggedIn) { | |
request(STATE.cssMapUrl, { responseType : 'json' }).then(function (res) { | |
cssMap = res.response; | |
console.log(cssMap); | |
// Wait for building the timeline | |
var timer = setInterval(function () { | |
var timeline = null; | |
for ( i = 0 ; i < cssMap.timeline.length ; i++ ) { | |
timeline = $X("//div[@class='" + cssMap.timeline[i] + "']/div[div[@data-id]]")[0]; | |
if ( timeline ) { | |
break; | |
} | |
} | |
if (!timeline) { | |
return; | |
} | |
clearInterval(timer); | |
for (var i = 0 ; i < cssMap.post.length ; i++) { | |
postClass = "[@class=\"" + cssMap.post[i] + "\"]"; | |
var link = $X('//article' + postClass + '/header//a[@aria-label="Permalink"]')[0]; | |
if (link) { | |
console.log("postClass", postClass); | |
break; | |
} | |
} | |
}, 500); | |
}); | |
} | |
addAround(Extractors['ReBlog - Dashboard'], 'check', function (proceed, args, target, methodName) { | |
var ctx = args[0]; | |
if (!(/(tumblr-beta\.com|tumblr\.com)\//).test(ctx.href)) { | |
return false; | |
} | |
var link = $X('./ancestor-or-self::article' + postClass + '/header//a[@aria-label="Permalink"]', ctx.target)[0]; | |
return link && link.href | |
}); | |
addAround(Extractors['ReBlog - Dashboard'], 'extract', function (proceed, args, target, methodName) { | |
var ctx = args[0]; | |
var post = $X('./ancestor-or-self::div[@data-id][descendant::article' + postClass + ']', ctx.target)[0]; | |
var link = $X('.//article' + postClass + '/header//a[@aria-label="Permalink"]', post)[0]; | |
return new Promise(function (resolve, reject) { | |
chrome.runtime.sendMessage(TBRL.id, { | |
request: 'extractByLink', | |
content: { | |
href: link.href, | |
post_id: post.getAttribute('data-id') | |
} | |
}, function (res) { | |
ctx.reblog_id = res.reblog_id; | |
ctx.reblog_key = res.reblog_key; | |
var that = Extractors['ReBlog']; | |
return resolve(that.getFormKeyAndChannelId().then(function () { | |
return that.extractByEndpoint(ctx, that.TUMBLR_URL + 'reblog/' + ctx.reblog_id + '/' + ctx.reblog_key); | |
})); | |
}); | |
}); | |
}); | |
addAround(UserScripts['Dashboard + Taberareloo'], 'getCurrentItem', function (proceed, args, target, methodName) { | |
var paragraphs = $X('//div[@data-id][descendant::article' + postClass + ']'), toplist = new Array(paragraphs.length); | |
var get_top = function (index) { | |
return toplist[index] || (toplist[index] = paragraphs[index].getBoundingClientRect().top); | |
}; | |
function bsearch(list, compare) { | |
var lower = -1, upper = list.length, mid = null; | |
while (lower + 1 !== upper) { | |
mid = Math.floor((lower + upper) / 2); | |
if (compare(mid) <= 0) { | |
lower = mid; | |
} else { | |
upper = mid; | |
} | |
} | |
return list[lower + 1]; | |
} | |
return bsearch(paragraphs, function (index) { | |
return (get_top(index) < 0) ? -1 : 1; | |
}); | |
}); | |
return; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment