Skip to content

Instantly share code, notes, and snippets.

@yuiAs
Last active July 19, 2019 16:54
Show Gist options
  • Select an option

  • Save yuiAs/6aa26c9afe4d1613bb93 to your computer and use it in GitHub Desktop.

Select an option

Save yuiAs/6aa26c9afe4d1613bb93 to your computer and use it in GitHub Desktop.
TweetDeck弄りたい
(function() {
chrome.runtime.onInstalled.addListener(function () {
var rule3 = {
priority: 110,
conditions: [
new chrome.declarativeWebRequest.RequestMatcher({
url: {
hostEquals: 'pbs.twimg.com',
pathContains: '/media/',
queryContains: 'name='
},
stages: [ 'onBeforeRequest' ]
})
],
actions: [
new chrome.declarativeWebRequest.RedirectByRegEx({
from: 'https:\/\/pbs\.twimg\.com\/media\/([^\.]+)(?:\.?.{0,3})(?:.+?)(?:format\=)(.+)(?:.+?)(?:name\=)(?:large|medium)',
to: 'https://pbs.twimg.com/media/$1.$2:orig'
}),
new chrome.declarativeWebRequest.RedirectByRegEx({
from: 'https:\/\/pbs\.twimg\.com\/media\/([^\.]+)(?:\.?.{0,3})(?:.+?)(?:format\=)(.+)(?:.+?)(?:name\=)(?:[0-9]{4,}x[0-9]{4,})',
to: 'https://pbs.twimg.com/media/$1.$2:orig'
}),
new chrome.declarativeWebRequest.RedirectByRegEx({
from: 'https:\/\/pbs\.twimg\.com\/media\/([^\.]+)(?:\.?.{0,3})(?:.+?)(?:format\=)(.+)(?:.+?)(?:name\=)(.+)',
// このフォーマットだと nameによっては表示できないことがあるので rule4で事前に弾く
to: 'https://pbs.twimg.com/media/$1.$2:$3'
}),
new chrome.declarativeWebRequest.IgnoreRules({
lowerPriorityThan: 110
})
]
};
var rule4 = {
priority: 111,
conditions: [
new chrome.declarativeWebRequest.RequestMatcher({
url: {
hostEquals: 'pbs.twimg.com',
pathContains: '/media/',
queryContains: 'format=',
// 120x120と 900x900は表示できないのは確認済 (07/2019)
// 900x900に関しては origとして扱う
urlMatches: 'https:\/\/pbs\.twimg\.com\/media\/([^\.]+)(?:\.?.{0,3})(?:.+?)(?:format\=)(.+)(?:.+?)(?:name\=)(?:120x120|900x900)'
},
stages: [ 'onBeforeRequest' ]
})
],
actions: [
new chrome.declarativeWebRequest.RedirectByRegEx({
from: 'https:\/\/pbs\.twimg\.com\/media\/([^\.]+)(?:\.?.{0,3})(?:.+?)(?:format\=)(.+)(?:.+?)(?:name\=)(?:900x900)',
to: 'https://pbs.twimg.com/media/$1.$2:orig'
}),
new chrome.declarativeWebRequest.IgnoreRules({
lowerPriorityThan: 111
})
]
};
var rule5 = {
priority: 100,
conditions: [
new chrome.declarativeWebRequest.RequestMatcher({
url: {
hostEquals: 'pbs.twimg.com',
pathContains: '/media/',
queryContains: 'format=',
},
stages: [ 'onBeforeRequest' ]
})
],
actions: [
new chrome.declarativeWebRequest.RedirectByRegEx({
from: 'https:\/\/pbs\.twimg\.com\/media\/([^\.]+)(?:\.?.{0,3})(?:.+?)(?:format\=)(.+)',
to: 'https://pbs.twimg.com/media/$1.$2:orig'
})
]
};
var rule1 = {
priority: 900,
conditions: [
new chrome.declarativeWebRequest.RequestMatcher({
url: {
hostEquals: 'pbs.twimg.com',
pathContains: '/media/',
},
stages: [ 'onBeforeRequest' ]
})
],
actions: [
new chrome.declarativeWebRequest.RedirectByRegEx({
from: 'https:\/\/pbs\.twimg\.com\/media\/(.+)\:(?:.+)',
to: 'https://pbs.twimg.com/media/$1:orig'
})
]
};
chrome.declarativeWebRequest.onRequest.removeRules(void 0, function () {
chrome.declarativeWebRequest.onRequest.addRules([ rule3, rule4, rule5 ]);
});
});
})();
(function () {
function injectionScripts() {
function overrideTwitterMedia(TD) {
//TD.services.TwitterMedia.SERVICES['twipple'] = /https?:\/\/p\.twipple\.jp\/(.+)/;
TD.services.TwitterMedia.SERVICES['gyazo'] = /https?:\/\/gyazo\.com\/(.+)/i;
TD.services.TwitterMedia.prototype.thumb_ = TD.services.TwitterMedia.prototype.thumb;
TD.services.TwitterMedia.prototype.thumb = function () {
switch (this.service) {
case 'twipple':
return `http://p.twpl.jp/show/thumb/${this.urlMatches[1]}`;
case 'gyazo':
return this.small();
}
return this.thumb_();
};
TD.services.TwitterMedia.prototype.small_ = TD.services.TwitterMedia.prototype.small;
TD.services.TwitterMedia.prototype.small = function () {
switch (this.service) {
case 'twipple':
return this.thumb();
case 'gyazo':
if (this.entity && this.entity.media_url_https) {
return this.entity.media_url_https;
} else {
// TODO: 500は元が pngの場合は -png.jpgしかない
// SSは pngが多いので pngに寄せておく
return `https://i.gyazo.com/thumb/500/${this.urlMatches[1]}-png.jpg`;
}
break;
}
return this.small_();
};
TD.services.TwitterMedia.prototype.medium_ = TD.services.TwitterMedia.prototype.medium;
TD.services.TwitterMedia.prototype.medium = function () {
switch (this.service) {
case 'twipple':
return `http://p.twpl.jp/show/large/${this.urlMatches[1]}`;
case 'gyazo':
return this.large();
}
return this.medium_();
};
TD.services.TwitterMedia.prototype.large_ = TD.services.TwitterMedia.prototype.large;
TD.services.TwitterMedia.prototype.large = function () {
switch (this.service) {
case 'twitter':
// 常に origを返す
// たまに origの生成が遅延している or キャッシュが行きわたってないことがあるけど
return this.getTwitterPreviewUrl(":orig");
case 'twipple':
return `http://p.twpl.jp/show/orig/${this.urlMatches[1]}`;
case 'gyazo':
// 1200はなぜか元が pngでも -jpg.jpgのファイルが存在する場合がある
return `https://i.gyazo.com/thumb/1200/${this.urlMatches[1]}-jpg.jpg`;
}
return this.large_();
};
}
function generateUUID() {
var s = '';
for (var i = 0; i < 32; ++i) {
var rand = Math.floor(Math.random() * 17);
s += ((i == 12) ? 4 : ((i == 16) ? (rand & 3 | 8) : rand)).toString(16);
}
return s;
}
function overrideTest(TD) {
TD.services.TwitterStatus.prototype.fromJSONObject_ = TD.services.TwitterStatus.prototype.fromJSONObject;
TD.services.TwitterStatus.prototype.fromJSONObject = function (e) {
if (e.card && e.card.binding_values) {
var b = e.card.binding_values;
if (b.domain && (b.domain.string_value == 'gyazo.com')) {
var s = b.photo_image_full_size_original || b.photo_image_full_size || {};
if (s.image_value && (s.type == 'IMAGE')) {
e.entities = e.entities || {};
e.entities.mediax = [];
e.entities.mediax.push({
media_url_https: s.image_value.url,
id_str: e.id_str || generateUUID()
});
}
}
}
return this.fromJSONObject_(e);
};
TD.services.TwitterStatus.prototype._extractEntities_ = TD.services.TwitterStatus.prototype._extractEntities;
TD.services.TwitterStatus.prototype._extractEntities = function (e) {
if (e.entities && e.entities.mediax) {
return e.entities;
} else {
return this._extractEntities_(e);
}
};
TD.services.TwitterMedia.prototype.getMediaFromEntities_ = TD.services.TwitterMedia.prototype.getMediaFromEntities;
TD.services.TwitterMedia.prototype.getMediaFromEntities = function (e, t) {
// 返ってくるのは Arrayで serivce=twitter, entity.type=videoのみ 1要素
var r = this.getMediaFromEntities_(e, t);
if (e.mediax && (e.mediax.length > 0)) {
r.forEach(function (v, i) {
if (v.service == 'gyazo') {
v.entity = v.entity || e.mediax[0];
}
});
}
return r;
};
// TD.services.TwitterClient.prototype.getListTimeline_ = TD.services.TwitterClient.prototype.getListTimeline;
// TD.services.TwitterClient.prototype.getListTimeline = function(e, t, i, n, s, r, o, a) {
// r = 1000;
// return this.getListTimeline_(e, t, i, n, s, r, o, a);
// };
}
// ページ読み込み直後には window.TDが存在せずしばらくして生成されるため少し待つ
setTimeout(function () {
var TD = window.TD;
if (TD === void 0) {
console.error('window.TD did not exist');
return;
} else {
overrideTest(TD);
overrideTwitterMedia(TD);
}
}, 1000);
}
// 一度ドキュメントノードに追加して即削除
var id = 'tdzero_injection_js';
if (!document.querySelector(`#${id}`)) {
var elem = document.createElement("script");
elem.type = 'text/javascript';
elem.id = id;
elem.innerHTML = `(${injectionScripts.toString()})();`;
document.body.appendChild(elem);
//document.body.removeChild(elem);
elem = null;
}
})();
{
"name": "TweetDeck -1",
"version": "0.0.1",
"manifest_version": 2,
"description": "TweetDeckを弄っていく",
"background": {
"persistent": false,
"scripts": [
"background.js"
]
},
"content_scripts": [
{
"matches": [
"https://tweetdeck.twitter.com/"
],
"js": [
"content.js"
]
}
],
"permissions":[
"declarativeWebRequest",
"*://*/*"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment