-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
機能追加(4/4): 短縮URLの展開機能を追加 #223
Conversation
😻 |
xhr = new XMLHttpRequest() | ||
if /// //t\.co/ ///.test(targetUrl) | ||
xhr.open("GET", targetUrl) | ||
xhr.responseType = "document" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t.co
の場合でも、HEADリクエストでlocation取得できるようですが、敢えて分けている理由を教えてください。
$ curl --head -I https://t.co/9fGZDZz3P2
HTTP/1.1 301 Moved Permanently
cache-control: private,max-age=300
content-length: 0
date: Sun, 20 Nov 2016 02:26:54 GMT
expires: Sun, 20 Nov 2016 02:31:54 GMT
location: https://readcrx-2.github.io/read.crx-2/
server: tsa_m
set-cookie: muc=63353035-57cf-4443-b3de-4f8f7cfc891a; Expires=Fri, 02 Nov 2018 02:26:54 UTC; Domain=t.co
strict-transport-security: max-age=0
x-connection-hash: 4cc500c45ecc8f6263fe0a890c43d822
x-response-time: 102
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
確認します。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
もしHEADだけになるならapp.HTML
を利用してください
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t.coの例外処理についてですが、XMLHttpRequest()でHEADリクエストをした場合は、何故かstatus=200でrespons headにlocationがありません。詳しい理由がわからないのでGETリクエストを使用しています。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
リクエスト時のUserAgentヘッダーを消すと301が戻ってきました
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
情報ありがとうございます。
curlコマンドとのヘッダーの違いはUser-AgentとCookieくらいだったので、実験して試そうと思っていました。
asyncProcessDfds = [] | ||
|
||
# 短縮URLの展開と追加 | ||
expandShortURL = (aSrc, targetUrl) -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
基本的には、スレッドの表示以外では使う機会がなさそうですが、
短縮URLの展開機能として独立させて、core/URL.ts
か、core/util.coffee
へ移動したほうがいいと思います。
@S--Minecraft ↑どう(どっちがいい)でしょう?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そうですね
core/URL.ts
がいいかなと
checkShortURL = (tUrl) -> | ||
if tmp = /// ^h?ttps?://([\w\-\.]+)/.+ ///.exec(tUrl) | ||
return true if shortUrlSites.indexOf(tmp[1]) >= 0 | ||
return false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可読性が若干損なわれますが、速度優先で正規表現判定一発にしちゃいましょう。
core/URL.ts
に移動をお願いします。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
了解しました。
expandedURL.setAttribute("short-url", sourceA.href) | ||
|
||
if finalUrl is "" | ||
expandedURL.appendChild(document.createTextNode("展開できませんでした。")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
クラスを追加してcssで::afterをつけるほうがよいのでは?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
やってみます。
expandedURL.appendChild(document.createTextNode("展開できませんでした。")) | ||
else | ||
expandedURLLink = document.createElement("a") | ||
expandedURLLink.appendChild(document.createTextNode(finalUrl)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expandedURLLink.textContent = finalUrl
で
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
はい。
xhr = new XMLHttpRequest() | ||
if /// //t\.co/ ///.test(targetUrl) | ||
xhr.open("GET", targetUrl) | ||
xhr.responseType = "document" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
もしHEADだけになるならapp.HTML
を利用してください
) | ||
else | ||
return d.resolve() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
app.util.concurrent
ではだめですか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
既存の方法ですっきりさせたいのですが、短縮URLの展開後のサムネイルの確認などがあり、それぞれの終了時の処理内容など、うまい方法がないか検討中です。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checkUrl = (a) ->
d = new $.Deffered()
if app.config.get("expand_short_url") isnt "none"
if app.url.checkShortURL(a.href)
app.url.expandShortURL(a, a.href).done( (a, finalUrl) ->
newLink = addExpandedURL(a, finalUrl)
if finalUrl
d.resolve(a, newLink)
else
d.resolve(a, a.href)
return
)
else
d.resolve(a, a.href)
else
d.resolve(a, a.href)
return d.promise()
app.util.concurrent(@container.querySelectorAll(".message > a:not(.thumbnail):not(.has_thumbnail)"), (a) ->
checkUrl(a).done( (a, link) ->
return app.ImageReplaceDat.do(a, link)
).done( (a, res, err) ->
# addThumbnail関連
)
)
これでどうでしょう?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
提案ありがとうございます。
非同期プログラミングに不慣れな身としては何がなんだか。と言った感じですが試してみます。
%br | ||
%label | ||
タイムアウト: | ||
%input.direct(type="number" name="expand_short_url_timeout" min="0" max="5000")ms |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
設定につくらずに固定値でいいのでは?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
元々は特にタイムアウトは指定していなかったのですが、時々異常に時間のかかるものがあることからタイムアウトの必要性を感じ追加しました。
正常に終了するまでの時間のばらつきが大きく、適正と思われる値を固定化するのが難しいと感じ設定可能としました。
修正しました。 変更箇所 補足 |
"t\.co|tiny\.cc|tinyurl\.com|tl\.gd|tr\.im|trib\.al|" + | ||
"url\.ie|urx\.nu|urx2\.nu|urx3\.nu|ur0\.pw|" + | ||
"wk\.tk|xrl\.us" + | ||
")\/.+"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これだと毎回正規表現コンパイルされるので、constとしてSHORT_URL_REG = /.../
としてください。
57-63行目の様な形で。
あと、.testしか使わないのであれば、関数化せずにそのままtestでもいいかと。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
処理効率を優先すれば定数化するのは良い選択だと思いますが、設定画面からの追加・変更の可能性は閉ざされます。よろしいですか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t.co
みたいに特殊な例もあるので、短縮URLは定数でいいと思います。
追加要望あれば都度変更でも対応可能ですし(少なくとも一般的によく使われる短縮URLサービス程度であれば)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
了解しました。
|
||
resonseHeaders = Request.parseHTTPHeader(xhr.getAllResponseHeaders()); | ||
|
||
callback(new Response(xhr.status, resonseHeaders, xhr.responseText)); | ||
callback(new Response(xhr.status, resonseHeaders, xhr.responseText, xhr2.responseURL)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typescriptのバグっぽいですね microsoft/TypeScript#11510
2.1.3で修正されるみたいなのでとりあえずのところ
一行目に///<reference path="../global.d.ts" />
と書いて
global.d.ts
に以下の記述をするとコンパイルを通せると思います
namespace XMLHttpRequest {
declare var responseURL: string;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上記の修正をしましたが、以下のエラーが発生します。
src/global.d.ts(7,11): error TS2300: Duplicate identifier 'XMLHttpRequest'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、namespace
じゃなくてinterface
でした
すいません…
|
||
return {requestHeaders: details.requestHeaders} | ||
,{ | ||
urls: ["<all_urls>"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
追加するときに追加すればよいのでは…?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
では現状ではt.coのみにします。
return | ||
|
||
# 短縮URLの展開でのt.coに対する例外 | ||
if details.method is "HEAD" and /// //t\.co/ ///.test(details.url) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
app.URL.getDomain(details.url) is "t.co"
で
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
了解しました。
) | ||
).always(=> | ||
d.resolve() | ||
# Audioの確認 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
元々Audio/VideoがImageReplaceDatの処理後だったのがそうでなくなってます
おそらく817行目のあとにくるべきかと
) | ||
else | ||
return d.resolve() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checkUrl = (a) ->
d = new $.Deffered()
if app.config.get("expand_short_url") isnt "none"
if app.url.checkShortURL(a.href)
app.url.expandShortURL(a, a.href).done( (a, finalUrl) ->
newLink = addExpandedURL(a, finalUrl)
if finalUrl
d.resolve(a, newLink)
else
d.resolve(a, a.href)
return
)
else
d.resolve(a, a.href)
else
d.resolve(a, a.href)
return d.promise()
app.util.concurrent(@container.querySelectorAll(".message > a:not(.thumbnail):not(.has_thumbnail)"), (a) ->
checkUrl(a).done( (a, link) ->
return app.ImageReplaceDat.do(a, link)
).done( (a, res, err) ->
# addThumbnail関連
)
)
これでどうでしょう?
指摘のあった箇所を修正しました。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
app.util.concurrent
の件一応テストだけお願いします(パパっと書いたものなので…)
|
||
resonseHeaders = Request.parseHTTPHeader(xhr.getAllResponseHeaders()); | ||
|
||
callback(new Response(xhr.status, resonseHeaders, xhr.responseText)); | ||
callback(new Response(xhr.status, resonseHeaders, xhr.responseText, xhr2.responseURL)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、namespace
じゃなくてinterface
でした
すいません…
コンパイル出来るようになりました。ありがとうございます。 |
checkUrl(a).done( (a, link) -> | ||
return app.ImageReplaceDat.do(a, link).done( (a, res, err) -> | ||
# サムネイルの追加 | ||
addThumbnail(a, res.text, "image", res.referrer, res.cookie) unless err? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unless err?
はaudio,videoの処理にもかけないといけないのでは?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
audioとvideoは、ImageViewURLReplace.dat
に依存しなため不要と考えます。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ImageViewURLReplace.dat
でurlをaudio/videoのmp4などへのurlに置き換えることもあるのでは?
".message > a:not(.thumbnail):not(.has_thumbnail):not(.expandedURL):not(.has_expandedURL)" | ||
), (a) -> | ||
checkUrl(a).done( (a, link) -> | ||
return app.ImageReplaceDat.do(a, link).done( (a, res, err) -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checkUrl(a).done( (a, link) ->
return app.ImageReplaceDat(a,link)
).done( (a,res,err) ->
〜
)
よくかんがえたらこうかけました
インデント減るのでこっちで…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
元々そのように書かれていましたが、サムネイルが表示されないため修正しました。
(a
は受け取れてもres
を受け取れないため?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
すいませんちょっといろんな仕様を混同してました
done
を両方ともthen
にするといけるはずです
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then
に変更して動くようになりました。
変更箇所 |
変更箇所 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
短縮URLの展開機能の追加です。
対象となるサイトは、t.coを除きヘッダのリクエストでLocation:が返ってくるものを対象にしています。
($ curl -I -i URL (linuxの場合) にて確認)
ただし、youtu.beのようなサムネイルの取得で対応可能と思われるものは含まれていません。
よろしくお願いします。