LoginSignup
2
1

More than 3 years have passed since last update.

Yahooニュースのコメントを一括取得(AutoPagerize)するブックマークレットを作ってみた

Last updated at Posted at 2019-04-10

pages.jpg

Yahooニュースの記事において上のように分割されているコメントを一気に読みたい。

前回、東洋経済オンラインのコメントを一括取得するブックマークレットを作ってみたが、Yahooニュース用にブックマークレットを作ってみる。

東洋経済オンラインのコメントはfetchで取得した。Yahooニュースのコメントはfetchで取得できなかったので、iframeで取得するようにした。

ブックマークレット

使い方

Yahooニュースの記事を開いて、すべてのコメントを読むをクリックしてから、ブックマークレットを動かす。ブックマークレットの作り方は省略。

  • const start_page = 1; コメントの開始ページ。
  • const page_times = 4; 取得ページ数。
  • const comment_num = 50; 1ページあたりのコメント数(元は10)。
  • const frame_height = "8000px"; ページのコメントの高さ(固定値)。
  • const sleep_time = 1000; 次のコメントを取得するまでの待機時間(ms)。

諸事情につき設定値を変更する必要がある。

fetch時のエラーメッセージ

main?d=20190412-00000001-it_nlab-bus_all:1 Access to fetch at 'https://news.yahoo.co.jp/comment/plugin/v1/full/' from origin 'https://headlines.yahoo.co.jp' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

VM312:1 Cross-Origin Read Blocking (CORB) blocked cross-origin response https://news.yahoo.co.jp/comment/plugin/v1/full/ with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.

CORS

ざっくりと言うと、次のような構成になっていると考えられる。
- 別サイトにあるものをJavaScriptで取得することを禁止。
- iframeのsrc指定で別サイトにあるものを取得することを許可。
- しかし、iframeの中で取得したテキストをJavaScriptで取得することを禁止。
- このような構成になったのは、iframeのsrc指定で不正アクセスさせられるから。例えばログイン必要なものをiframeのsrc指定して操作できたらアクセス権が奪えて危ない。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1