Create a gist now

Instantly share code, notes, and snippets.

Taberareloo Patch: Fix Twitter.update 2016.08
// ==Taberareloo==
// {
// "name" : "Fix Twitter.update 2016.08"
// , "description" : "Fix Twitter.update 2016.08"
// , "include" : ["background"]
// , "version" : "0.2.0"
// , "downloadURL" : "https://gist.githubusercontent.com/ailispaw/9406c2ffacf68c6ad238/raw/patch.fix.twitter.update.tbrl.js"
// }
// ==/Taberareloo==
(function() {
addAround(Models['Twitter'], 'update', function (proceed, args, target, methodName) {
var self = target;
var status = args[0];
var media_ids = args[1];
return self.getToken().then(function (token) {
var sendContent = {
authenticity_token : token.authenticity_token,
is_permalink_page : false,
place_id : '',
status : status,
tagged_users : ''
};
if (media_ids) {
sendContent.media_ids = media_ids;
}
self.addBeforeSendHeader();
// FIXME: 403が発生することがあったため redirectionLimit:0 を外す
return request(self.URL + '/i/tweet/create', {
sendContent : sendContent,
responseType: 'json',
headers : {
"x-requested-with" : "XMLHttpRequest"
}
}).then(function (res) {
self.removeBeforeSendHeader();
}).catch(function (res) {
self.removeBeforeSendHeader();
var json = res.response || res;
throw new Error(json.message);
});
});
})
update(Models['Twitter'], {
UPLOAD_URL : 'https://upload.twitter.com/i/media/upload.json',
addBeforeSendHeader : function() {
chrome.webRequest.onBeforeSendHeaders.addListener(
this.setOriginHeader,
{
urls: [
this.LINK + '*',
this.UPLOAD_URL + '*'
]
},
[ "blocking", "requestHeaders" ]
);
},
removeBeforeSendHeader : function() {
chrome.webRequest.onBeforeSendHeaders.removeListener(
this.setOriginHeader,
{
urls: [
this.LINK + '*',
this.UPLOAD_URL + '*'
]
},
[ "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;
},
setOriginHeader : function(details) {
var headers = details.requestHeaders;
headers = Models['Twitter'].setHTTPHeader(headers, 'Referer', 'https://twitter.com/');
headers = Models['Twitter'].setHTTPHeader(headers, 'Origin', 'https://twitter.com');
return {requestHeaders: headers};
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment