Skip to content

Instantly share code, notes, and snippets.

@YungSang
Last active April 6, 2017 03:45
Show Gist options
  • Save YungSang/9205023 to your computer and use it in GitHub Desktop.
Save YungSang/9205023 to your computer and use it in GitHub Desktop.
Taberareloo 用パッチ: Twitter の投稿仕様の変更に対応
// ==Taberareloo==
// {
// "name" : "Patch for Twitter Model"
// , "description" : "Patch for Twitter model"
// , "include" : ["background"]
// , "version" : "0.2.2"
// , "downloadURL" : "https://gist.github.com/YungSang/9205023/raw/patch.model.twitter.tbrl.js"
// }
// ==/Taberareloo==
(function () {
var version = chrome.runtime.getManifest().version;
version = version.split('.');
if (version.length > 3) {
version.pop();
}
version = version.join('.');
if (semver.gte(version, '3.0.12')) {
setTimeout(function () {
Patches.uninstall(Patches['patch.model.twitter.tbrl.js'], true);
}, 500);
Patches.install('https://raw.githubusercontent.com/YungSang/patches-for-taberareloo/ready-for-v4.0.0/patches/patch.model.twitter.createstatus.tbrl.js', true);
return;
}
setTimeout(function () {
if (Patches['patch.model.twitter.upload.tbrl.js']) {
Patches.uninstall(Patches['patch.model.twitter.upload.tbrl.js'], true);
}
if (Patches['patch.model.twitter.createstatus.tbrl.js']) {
Patches.uninstall(Patches['patch.model.twitter.createstatus.tbrl.js'], true);
}
}, 500);
addAround(Models['Twitter'], 'createStatus', function (proceed, args, target, methodName) {
var ps = update({}, args[0]);
if (ps.body) {
ps.body = ps.body.trimTag().replace(/\s+/g, ' ');
}
return proceed([ps]).addCallback(function (status) {
return status;
}).addErrback(function(e) {
console.log(e.message);
var over = '';
if (typeof e.message === 'string') {
over = e.message.extract(/post \((\d+) over\)/);
}
if (!over) {
throw e;
}
var len;
if (ps.body) {
len = ps.body.length;
ps.body = ps.body.slice(0, -1 * over);
over -= len;
}
if ((over > 0) && ps.item) {
len = ps.item.length;
ps.item = ps.item.slice(0, -1 * over);
over -= len;
}
if ((over > 0) && ps.description) {
len = ps.description.length;
ps.description = ps.description.slice(0, -1 * over);
over -= len;
}
if (over > 0) {
throw e;
}
return target[methodName](ps).addCallback(function (status) {
return status;
});
});
});
update(Models['Twitter'], {
update : function (status, media_ids) {
var self = this;
return this.getToken().addCallback(function (token) {
var sendContent = {
authenticity_token : token.authenticity_token,
place_id : '',
status : status,
tagged_users : ''
};
if (media_ids) {
sendContent.media_ids = media_ids;
}
return request(self.URL + '/i/tweet/create', {
sendContent : sendContent
}).addErrback(function (e) {
var res = e.message;
var json = res.responseText;
try {
json = JSON.parse(json);
throw new Error(json.message);
} catch (e2) {
throw e2;
}
});
});
},
upload : function (ps, status, file) {
var self = this;
var UPLOAD_URL = 'https://upload.twitter.com/i/media/upload.iframe';
var SIZE_LIMIT = 3145728;
return this.getToken().addCallback(function (token) {
return fileToBinaryString(file).addCallback(function (binary) {
return request(UPLOAD_URL, {
queryString : {
origin : self.URL
},
sendContent : {
authenticity_token : token.authenticity_token,
iframe_callback : '',
media : window.btoa(binary),
upload_id : (new Date()).getTime(),
origin : self.URL
}
}).addCallback(function (res) {
var html = res.responseText;
var json = html.extract(/parent\.postMessage\(JSON\.stringify\((\{.+\})\), ".+"\);/);
json = JSON.parse(json);
return self.update(status, json.media_id_string);
});
});
});
},
getActualLength : function (status) {
var ret = status.split('\n').map(function (s) {
s = s.replace(/(https:\/\/[^ ]+)/g, '12345678901234567890123');
s = s.replace(/(http:\/\/[^ ]+)/g, '1234567890123456789012');
return s;
}).join('\n');
return ret.length;
}
});
})();
// ==Taberareloo==
// {
// "name" : "Upload an image to Twitter"
// , "description" : "Upload an image to Twitter"
// , "include" : ["background"]
// , "version" : "0.3.1"
// , "downloadURL" : "https://gist.github.com/YungSang/9205023/raw/patch.model.twitter.upload.tbrl.js"
// }
// ==/Taberareloo==
(function() {
setTimeout(function () {
if (Patches['patch.model.twitter.upload.tbrl.js']) {
Patches.uninstall(Patches['patch.model.twitter.upload.tbrl.js'], true);
}
}, 500);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment