Instantly share code, notes, and snippets.

Embed
What would you like to do?
Taberareloo Patch: Fix Twitter.update 2018.04
// ==Taberareloo==
// {
// "name" : "Fix Twitter.update 2018.04"
// , "description" : "Fix Twitter.getToken 2018.04"
// , "include" : ["background"]
// , "version" : "0.3.1"
// , "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'
// , 'upgrade-insecure-requests': 1
}
}).then(function (res) {
self.removeBeforeSendHeader();
}).catch(function (res) {
self.removeBeforeSendHeader();
var json = res.response || res;
throw new Error(json.message);
});
});
})
addAround(Models['Twitter'], 'getToken', function (proceed, args, target, methodName) {
var self = target;
self.addBeforeSendHeader();
var promise = new Promise(function (resolve, reject) {
chrome.cookies.get({
url: 'https://twitter.com',
name: 'csrf_same_site'
}, function (cookie) {
if (cookie) {
chrome.cookies.remove({
url: 'https://twitter.com',
name: 'csrf_same_site',
storeId: cookie.storeId
}, function (details) {
chrome.cookies.set({
url: 'https://twitter.com',
name: 'csrf_same_site',
value: '1',
domain: '.twitter.com',
path: '/',
secure: true,
httpOnly: true,
expirationDate: cookie.expirationDate
}, function (cookie) {
return resolve();
});
});
} else {
chrome.cookies.set({
url: 'https://twitter.com',
name: 'csrf_same_site',
value: '1',
domain: '.twitter.com',
path: '/',
secure: true,
httpOnly: true
}, function (cookie) {
return resolve();
});
}
});
});
return promise.then(function () {
return request(self.URL + '/settings/account', {
responseType: 'document',
headers : {
// 'upgrade-insecure-requests': 1
}
}).then(function (res) {
self.removeBeforeSendHeader();
var doc = res.response;
if (!$X('//body[contains(concat(" ",normalize-space(@class)," "), " logged-in ")]', doc)[0]) {
throw new Error(chrome.i18n.getMessage('error_notLoggedin', self.name));
}
return {
authenticity_token : $X('input[@class="authenticity_token"]/@value', doc)[0]
};
}).catch(function (err) {
self.removeBeforeSendHeader();
throw new Error(err.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