Skip to content

Instantly share code, notes, and snippets.

@ailispaw
Last active April 11, 2022 03:07
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ailispaw/b5b2b65fddcb70f03e298a15403bfa8e to your computer and use it in GitHub Desktop.
Save ailispaw/b5b2b65fddcb70f03e298a15403bfa8e to your computer and use it in GitHub Desktop.
Taberareloo: Fix Tumblr.post 2021.12
// ==Taberareloo==
// {
// "name" : "Fix Tumblr.post 2021.12"
// , "description" : "Fix Tumblr.post 2021.12"
// , "include" : ["background", "content"]
// , "match" : ["*://*/*"]
// , "version" : "0.4.3"
// , "downloadURL" : "https://gist.githubusercontent.com/ailispaw/b5b2b65fddcb70f03e298a15403bfa8e/raw/patch.fix.tumblr.post.tbrl.js"
// }
// ==/Taberareloo==
(function() {
if (inContext('background')) {
addAround(Tumblr, '_post', function (proceed, args, target, methodName) {
var self = target;
var form = args[0];
return self.queue.push(function () {
self.addBeforeSendHeader();
return (form.form_key ? Promise.resolve( form.form_key ) : request(Tumblr.TUMBLR_URL + 'neue_web/iframe/new/text', {
responseType: 'document'
}).then( function ( res ) {
var doc = res.response;
return $X('//meta[@id="tumblr_form_key"]/@content', doc)[0];
})).then( function ( form_key ) {
return request(Tumblr.TUMBLR_URL + 'svc/secure_form_key', {
method : 'POST',
headers : {
'X-tumblr-form-key' : form_key
}
}).then(function (res) {
var secure_form_key = res.getResponseHeader('X-tumblr-secure-form-key');
return request(Tumblr.TUMBLR_URL + 'svc/post/update', {
headers : {
'Content-Type' : 'application/json',
'X-requested-with' : 'XMLHttpRequest',
'X-tumblr-form-key' : form_key,
'X-tumblr-puppies' : secure_form_key
},
sendContent : JSON.stringify(form)
}).then(function (res) {
self.removeBeforeSendHeader();
var json = JSON.parse(res.responseText);
if (json && json.errors) {
throw new Error(json.errors[0].message);
}
return res;
}).catch(function (res) {
self.removeBeforeSendHeader();
return res;
});
});
});
});
});
addAround(Tumblr, 'getForm', function (proceed, args, target, methodName) {
var self = target;
self.addBeforeSendHeader();
return (Tumblr.form_key ? Promise.resolve( Tumblr.form_key ) : request(Tumblr.TUMBLR_URL + 'neue_web/iframe/new/text', {
responseType: 'document'
}).then( function ( res ) {
var doc = res.response;
return $X('//meta[@id="tumblr_form_key"]/@content', doc)[0];
})).then( function ( form_key ) {
self.removeBeforeSendHeader();
Tumblr.form_key = form_key;
var form = {
form_key: form_key,
channel_id: self.channel_id,
context_id: '',
context_page: 'dashboard',
custom_tweet: '',
'post[date]': '',
'post[draft_status]': '',
'post[publish_on]': '',
'post[slug]': '',
'is_rich_text[one]': '0',
'is_rich_text[three]': '0',
'is_rich_text[two]': '0',
'post[state]': '0',
allow_photo_replies: '',
send_to_fbog: TBRL.Config.entry.tumblr2facebook ? 'on' : '',
send_to_twitter: TBRL.Config.entry.tumblr2twitter ? 'on' : ''
};
if (form.channel_id) {
return form;
}
return Models.getMultiTumblelogs(true).then(function () {
form.channel_id = Tumblr.channel_id;
return form;
});
});
});
update(Tumblr, {
addBeforeSendHeader : function() {
try {
chrome.webRequest.onBeforeSendHeaders.addListener(
this.setOriginHeader,
{
urls: [
this.LINK + '*',
]
},
[ "blocking", "requestHeaders", "extraHeaders" ]
);
} catch (e) {
chrome.webRequest.onBeforeSendHeaders.addListener(
this.setOriginHeader,
{
urls: [
this.LINK + '*',
]
},
[ "blocking", "requestHeaders" ]
);
}
},
removeBeforeSendHeader : function() {
try {
chrome.webRequest.onBeforeSendHeaders.removeListener(
this.setOriginHeader,
{
urls: [
this.LINK + '*',
]
},
[ "blocking", "requestHeaders", "extraHeaders" ]
);
} catch (e) {
chrome.webRequest.onBeforeSendHeaders.removeListener(
this.setOriginHeader,
{
urls: [
this.LINK + '*',
]
},
[ "blocking", "requestHeaders" ]
);
}
},
setHTTPHeader : function(headers, name, value) {
var found = false;
for (var i = 0; i < headers.length; ++i) {
if (headers[i].name === name) {
headers[i].value = value;
found = true;
break;
}
}
if (!found) {
headers.push({
name : name,
value : value
});
}
return headers;
},
delHTTPHeader : function(headers, name) {
var hdrs = [];
for (var i = 0; i < headers.length; ++i) {
if (headers[i].name !== name) {
hdrs.push({
name : headers[i].name,
value : headers[i].value
});
}
}
return hdrs;
},
setOriginHeader : function(details) {
var headers = details.requestHeaders;
headers = Tumblr.setHTTPHeader(headers, 'Referer', 'https://www.tumblr.com/');
headers = Tumblr.setHTTPHeader(headers, 'Origin', 'https://www.tumblr.com/');
return {requestHeaders: headers};
}
});
setTimeout(function() {
Models.getMultiTumblelogs();
}, 1000);
TBRL.setRequestHandler('getTumblrPostFetch', function (req, sender, func) {
var ctx = req.content;
Tumblr.addBeforeSendHeader();
return request(Tumblr.TUMBLR_URL + 'svc/post/fetch', {
queryString: {
reblog_id: ctx.reblog_id,
reblog_key: ctx.reblog_key
},
responseType: 'json'
}).then(function (res) {
Tumblr.removeBeforeSendHeader();
func({
response : res.response
});
}).catch(function (err) {
return request(Tumblr.TUMBLR_URL + 'svc/post/fetch', {
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
sendContent: JSON.stringify({
form_key: ctx.form_key,
reblog_id: ctx.reblog_id,
reblog_key: ctx.reblog_key,
post_type: ctx.post_type
}),
responseType: 'json'
}).then(function (res) {
Tumblr.removeBeforeSendHeader();
func({
response : res.response
});
}).catch(function (err) {
Tumblr.removeBeforeSendHeader();
func({
err : err
});
});
});
});
return;
}
if (inContext('content')) {
addAround(Extractors['ReBlog'], 'getForm', function (proceed, args, target, methodName) {
var that = target;
var ctx = args[0];
var url = args[1];
return new Promise(function (resolve, reject) {
chrome.runtime.sendMessage(TBRL.id, {
request: 'getTumblrPostFetch',
content: {
form_key: that.form_key,
reblog_id: ctx.reblog_id,
reblog_key: ctx.reblog_key,
post_type: ctx.post_type
}
}, function (res) {
if (res.response) {
var response = res.response;
var post = response.post;
var form = {
form_key: that.form_key,
channel_id: that.channel_id,
detached: true,
reblog: true,
reblog_id: ctx.reblog_id,
reblog_key: ctx.reblog_key,
errors: false,
created_post: true,
context_page: 'dashboard',
post_context_page: response.post_context_page,
silent: true,
context_id: '',
reblog_post_id: post.reblog_source.split('/post/')[1].split('/')[0],
'is_rich_text[one]': '0',
'is_rich_text[two]': '0',
'is_rich_text[three]': '0',
'post[slug]': post.slug,
'post[source_url]': post.source_url || 'http://',
'post[date]': '',
'post[type]': post.type,
'post[one]': post.one,
'post[two]': post.two,
'post[three]': post.three,
'post[tags]': post.tags || '',
'post[publish_on]': '',
'post[state]': String(post.state),
custom_tweet: '',
allow_photo_replies: '',
send_to_fbog: TBRL.config.entry.tumblr2facebook ? 'on' : '',
send_to_twitter: TBRL.config.entry.tumblr2twitter ? 'on' : ''
};
if (post.type === 'photo') {
form['post[photoset_layout]'] = post.photoset_layout;
form['post[photoset_order]'] = [];
post.photos.forEach(function (photo) {
var id = photo.id;
form['post[photoset_order]'].push(id);
form['images[' + id + ']'] = '';
});
form['post[photoset_order]'] = form['post[photoset_order]'].join(',');
form.image = post.photos[0].url;
} else if (post.type === 'audio') {
form['id3_tags[album]'] = post.id3_tags.Album || '';
form['id3_tags[artist]'] = post.id3_tags.Artist || '';
form['id3_tags[title]'] = post.id3_tags.Title || '';
form.album_art = '';
form.pre_upload = '';
form.preuploaded_url = '';
form.remove_album_art = '';
} else if (post.type === 'video') {
form.keep_video = '';
form.pre_upload = '';
form.preuploaded_ch = '';
form.preuploaded_url = '';
form.valid_embed_code = '';
}
/* doesn't work any longer
return resolve(defer().then(function afterPhoto() {
if (!(TBRL.config.entry.not_convert_text && form['post[type]'] === 'link')) {
return form;
}
return request($N('a', {href: ctx.href}).origin + '/api/read', {
queryString: {
id: ctx.reblog_id
}
}).then(function (res) {
var xml = res.responseXML;
if (xml.querySelector('post').getAttribute('type') === 'regular') {
ctx.post_type = 'text';
return that.getForm(ctx, url);
}
return form;
});
}));
*/
return resolve(form);
} else {
if (that.retry) {
return reject(res.err.message);
}
that.form_key = that.channel_id = null;
return resolve(that.getCache(true).then(function (info) {
that.form_key = info.form_key;
that.channel_id = info.channel_id;
that.retry = true;
return that.extractByEndpoint(ctx, url);
}));
}
});
});
});
return;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment