---
title: Webページの更新を検知するツールを作った話
tags: 
author: [とらのあなラボ](https://www.docswell.com/user/toranoana_lab)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/GEWPXML8J2.jpg?width=480
description: とらのあなラボTechConferenceVol.2におけるLT大会「Webページの更新を検知するツールを作った話」の登壇資料です。 ■イベント情報 https://yumenosora.connpass.com/event/241175/ ■今後のイベントについてはこちら https://yumenosora.connpass.com/ ■虎の穴ラボ 採用サイト https://yumenosora.co.jp/tora-lab/
published: December 12, 24
canonical: https://www.docswell.com/s/toranoana_lab/KEXYGN-2024-12-12-110802
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/GEWPXML8J2.jpg)

-
Web ページの更新を検知するツール
を作った話
〜Web ページが更新されたら LINE でお知らせ！！〜
虎の穴ラボ株式会社 古賀広隆
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 2

![Page Image](https://bcdn.docswell.com/page/47ZK6QM973.jpg)

自己紹介
所属
虎の穴ラボ株式会社
とらのあな通販コスト削減チーム
主な担当
フロントエンド、サーバサイド
推し
南條愛乃さんです！
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 3

![Page Image](https://bcdn.docswell.com/page/YJ6Q26NDJV.jpg)

Webページの更新を検知するツールを作った話
子供の声が入るかも
100% フルリモートワーク で、今日も三重県の自宅から配信して
います。
ご了承ください
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 4

![Page Image](https://bcdn.docswell.com/page/GJ5R265874.jpg)

Webページの更新を検知するツールを作った話
あじぇんだ！
1. なぜ作成したか？
2. 各種フレームワーク／ライブラリの選定
3. 処理の流れ
4. 最後に
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 5

![Page Image](https://bcdn.docswell.com/page/LE3PKQNGJ5.jpg)

Webページの更新を検知するツールを作った話
1. なぜ作成したか？
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 6

![Page Image](https://bcdn.docswell.com/page/8EDL39WNJG.jpg)

Webページの更新を検知するツールを作った話
Web ページに変化があったら差分を画像で知りたいことがあった
ので、個人的に作りました。
いきなり、個人 Web サイトに異常があったりとか
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 7

![Page Image](https://bcdn.docswell.com/page/V7P24Z9N78.jpg)

Webページの更新を検知するツールを作った話
2. 各種フレームワーク／ライブラリの選定
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 8

![Page Image](https://bcdn.docswell.com/page/2JVDXWMYJQ.jpg)

Webページの更新を検知するツールを作った話
言語
Node.js &amp; TypeScript
→ 得意なので
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 9

![Page Image](https://bcdn.docswell.com/page/5EGYVDGWEL.jpg)

Webページの更新を検知するツールを作った話
クラウド環境
Cloud Storage for Firebase
→ 得意なので
Firebase Admin SDK
→ 画像をアップロード
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 10

![Page Image](https://bcdn.docswell.com/page/4JQM61XQ7P.jpg)

Webページの更新を検知するツールを作った話
ライブラリ
LINE Bot SDK → LINE 通知に
Playwright → Web サイトのスクショを撮る
pngjs → 過去のスクショ（PNG）画像の読み込み
pixelmatch → 過去のスクショと最新画像を比較する
sharp → LINE 通知用に画像をリサイズ
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 11

![Page Image](https://bcdn.docswell.com/page/K74V4R6YJ1.jpg)

システム構成イメージ
⑤ スクショのURLの
内容を取得
Saturday 10:12 AM
Hey man, got a sec?
Hi Tim, of course, just give
me a couple minutes to
ﬁnish breakfast.
Read Friday
LINE Messaging API
Cloud
Storage
③ スクショと差分を
アップロード
（公開）
④ スクショのURLと
メッセージをAPIに送
信
① スクショ取得
＆保存
Web
サイト等
RaspberryPi
② 画像の差分をとる
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 12

![Page Image](https://bcdn.docswell.com/page/LJ134QVN7G.jpg)

Webページの更新を検知するツールを作った話
3. 処理の流れ
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 13

![Page Image](https://bcdn.docswell.com/page/GJWPXM3M72.jpg)

Webページの更新を検知するツールを作った話
3-1. Playwright で対象の Web ページを開く
const browser = await playwright.chromium.launch({
args: [&quot;--ignore-certificate-errors&quot;, &quot;--lang=ja,en-US,en&quot;],
executablePath: &quot;/usr/bin/chromium-browser&quot;,
});
// Basic
const context = await browser.newContext({
httpCredentials: {
username: &quot;xxxxxxxx&quot;,
password: &quot;xxxxxxxx&quot;,
},
});
const page = await context.newPage();
await page.goto(new URL(url).href); // Web
URL
await page.waitForLoadState(&quot;domcontentloaded&quot;);
認証がある場合
サイトの を指定する
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 14

![Page Image](https://bcdn.docswell.com/page/4EZK6QVME3.jpg)

Webページの更新を検知するツールを作った話
3-2. スクショを撮る
await page.waitForTimeout(3000); //
await page.screenshot({
fullPage: true,
path: `${id}.new.png`,
});
たまにロード完了してないときがある
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 15

![Page Image](https://bcdn.docswell.com/page/Y76Q26557V.jpg)

Webページの更新を検知するツールを作った話
3-3. 前回実行時の画像ファイルがあったら PNG
を読み込む
if (fs.existsSync(`${id}.old.png`) &amp;&amp; fs.existsSync(`${id}.new.png`)) {
//
const file1 = fs.readFileSync(`${id}.old.png`)
const file2 = fs.readFileSync(`${id}.new.png`)
const img1 = PNG.sync.read(file1)
const img2 = PNG.sync.read(file2)
const { width, height } = img2
const diff2 = new PNG({ width, height })
新旧ファイルが揃っているときは、比較する
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 16

![Page Image](https://bcdn.docswell.com/page/G75R26YGE4.jpg)

Webページの更新を検知するツールを作った話
3-4. 比較して、比較結果を画像ファイルで書き出
す
const result2 = pixelmatch(img1.data, img2.data, diff2.data, width, height);
fs.writeFileSync(`${id}.diff2.png`, PNG.sync.write(diff2));
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 17

![Page Image](https://bcdn.docswell.com/page/9J2D4MGDJR.jpg)

比較結果画像のサンプル
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 18

![Page Image](https://bcdn.docswell.com/page/DEY2MXVMEM.jpg)

Webページの更新を検知するツールを作った話
3-5. 閾値以上の差分があったら、サムネイル画像
作成
if (result2 &gt; threshold) {
//
sharp(`${id}.new.png`)
.resize(512) //
512px
.toFile(`${id}.new.512.png`, (err, info) =&gt; {
if (err) {
throw err
}
console.log(info)
})
サムネイル向けの画像を作る
横幅 にリサイズ
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 19

![Page Image](https://bcdn.docswell.com/page/VJN8W1M378.jpg)

Webページの更新を検知するツールを作った話
3-6. 差分ファイルと最新のファイルを Firebase
にアップロード
最新のスクショ画像をアップロード
//
const respNew = await getStorage()
.bucket()
.upload(`${id}.new.png`, {
destination: `${id}-${YYYYMMDDHHmmss}-new.png`,
});
//
const respNew512 = await getStorage()
.bucket()
.upload(`${id}.new.512.png`, {
destination: `${id}-${YYYYMMDDHHmmss}-new-512.png`,
});
最新のスクショ画像のサムネイルをアップロード
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 20

![Page Image](https://bcdn.docswell.com/page/YE9KX5NPE3.jpg)

Webページの更新を検知するツールを作った話
3-7. LINE に通知を送る
const client = new line.Client(clientConfig.default);
const lineRespNew = await client.broadcast({
type: &quot;image&quot;,
originalContentUrl: respNew[0].publicUrl(),
previewImageUrl: respNew512[0].publicUrl(),
});
const lineRespText = await client.broadcast({
type: &quot;text&quot;,
text: url,
});
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 21

![Page Image](https://bcdn.docswell.com/page/GE8R2YKXJD.jpg)

Webページの更新を検知するツールを作った話
3-8. 旧ファイルを削除、最新ファイルをリネーム
して保存する
fs.unlinkSync(`${id}.old.png`);
fs.renameSync(`${id}.new.png`, `${id}.old.png`);
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 22

![Page Image](https://bcdn.docswell.com/page/LELK24ZNJR.jpg)

Webページの更新を検知するツールを作った話
4. さいごに！
LINE Messaging API は、登録すると無料で使えます。
また、友達や家族との共有も QR コードや URL でできます。
他にも、LINE Messaging API を使って作りたい。
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 23

![Page Image](https://bcdn.docswell.com/page/4JM28KVQJW.jpg)

Webページの更新を検知するツールを作った話
4. さいごに！
LINE 通知は他にも、とらのあな通販の告知とかセール、新発売、
配送状況、購入通知の通知などの機能開発に使えるかも？と思い
ました。
画像の差分もわかりやすくて、家族には好評でした。
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 24

![Page Image](https://bcdn.docswell.com/page/PJRM5X2KE9.jpg)

Webページの更新を検知するツールを作った話
ご清聴、ありがとうございました！
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 25

![Page Image](https://bcdn.docswell.com/page/PEXVK6D57X.jpg)

Appendix：ソースコード全録
import * as playwright from &quot;playwright-chromium&quot;;
import * as fs from &quot;fs&quot;;
import { PNG } from &quot;pngjs&quot;;
import * as pixelmatch from &quot;pixelmatch&quot;;
import { exit } from &quot;process&quot;;
import { initializeApp, applicationDefault } from &quot;firebase-admin/app&quot;;
import { getStorage } from &quot;firebase-admin/storage&quot;;
import * as line from &quot;@line/bot-sdk&quot;;
import * as clientConfig from &quot;./channel-access-token&quot;;
import * as sharp from &quot;sharp&quot;;
const getNewScreenShot = async (id: string, url: string) =&gt; {
const browser = await playwright.chromium.launch({
args: [&quot;--ignore-certificate-errors&quot;, &quot;--lang=ja,en-US,en&quot;],
executablePath: &quot;/usr/bin/chromium-browser&quot;,
});
const context = await browser.newContext({
httpCredentials: {
username: &quot;xxxxxxx&quot;,
password: &quot;xxxxxxx&quot;,
},
});
const page = await context.newPage();
await page.goto(new URL(url).href);
await page.waitForLoadState(&quot;domcontentloaded&quot;);
await page.waitForTimeout(3000);
await page.screenshot({
fullPage: true,
path: `${id}.new.png`,
});
};
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 26

![Page Image](https://bcdn.docswell.com/page/3EKQ51XRED.jpg)

Appendix：ソースコード全録
const sendLineMessage = async (id: string, url: string) =&gt; {
sharp(`${id}.new.png`)
.resize(512)
.toFile(`${id}.new.512.png`, (err, info) =&gt; {
if (err) {
throw err
}
console.log(info)
})
sharp(`${id}.diff2.png`)
.resize(512)
.toFile(`${id}.diff2.512.png`, (err, info) =&gt; {
if (err) {
throw err
}
console.log(info)
})
const YYYYMMDDHHmmss = new Date()
.toISOString()
.replace(/[^\d]/g, &#039;&#039;)
.slice(0, 14)
initializeApp({
credential: applicationDefault(),
storageBucket: &#039;change-web-page-reminder.appspot.com&#039;,
})
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 27

![Page Image](https://bcdn.docswell.com/page/L73PKQ5GE5.jpg)

Appendix：ソースコード全録
const respOld = await getStorage()
.bucket()
.upload(`${id}.old.png`, {
destination: `${id}-${YYYYMMDDHHmmss}-old.png`,
});
console.log(&quot;old publicUrl&quot;, respOld[0].publicUrl());
const respNew = await getStorage()
.bucket()
.upload(`${id}.new.png`, {
destination: `${id}-${YYYYMMDDHHmmss}-new.png`,
});
console.log(&quot;new publicUrl&quot;, respNew[0].publicUrl());
const respNew512 = await getStorage()
.bucket()
.upload(`${id}.new.512.png`, {
destination: `${id}-${YYYYMMDDHHmmss}-new-512.png`,
});
console.log(&quot;new publicUrl&quot;, respNew512[0].publicUrl());
const respDiff = await getStorage()
.bucket()
.upload(`${id}.diff2.png`, {
destination: `${id}-${YYYYMMDDHHmmss}-diff2.png`,
});
console.log(&quot;diff publicUrl&quot;, respDiff[0].publicUrl());
const respDiff512 = await getStorage()
.bucket()
.upload(`${id}.diff2.512.png`, {
destination: `${id}-${YYYYMMDDHHmmss}-diff2-512.png`,
});
console.log(&quot;diff publicUrl&quot;, respDiff512[0].publicUrl());
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 28

![Page Image](https://bcdn.docswell.com/page/87DL39DNEG.jpg)

Appendix：ソースコード全録
}
const client = new line.Client(clientConfig.default)
const lineRespNew = await client.broadcast({
type: &#039;image&#039;,
originalContentUrl: respNew[0].publicUrl(),
previewImageUrl: respNew512[0].publicUrl(),
})
console.log(lineRespNew)
const lineRespDiff = await client.broadcast({
type: &#039;image&#039;,
originalContentUrl: respDiff[0].publicUrl(),
previewImageUrl: respDiff512[0].publicUrl(),
})
console.log(lineRespDiff)
const lineRespText = await client.broadcast({
type: &#039;text&#039;,
text: url,
})
console.log(lineRespText)
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 29

![Page Image](https://bcdn.docswell.com/page/VJP24ZXNJ8.jpg)

Appendix：ソースコード全録
const main = async (id: string, url: string, threshold = 0) =&gt; {
// url
await getNewScreenShot(id, url)
if (fs.existsSync(`${id}.old.png`) &amp;&amp; fs.existsSync(`${id}.new.png`)) {
//
const file1 = fs.readFileSync(`${id}.old.png`)
const file2 = fs.readFileSync(`${id}.new.png`)
const img1 = PNG.sync.read(file1)
const img2 = PNG.sync.read(file2)
const { width, height } = img2
const diff2 = new PNG({ width, height })
console.log(&#039;pixelmatch2&#039;, img1.width, img1.height, img2.width, img2.height)
const result2 = pixelmatch(
img1.data,
img2.data,
diff2.data,
width,
height,
{
// threshold: 0.1,
// includeAA: true,
// alpha: 0.5,
// aaColor: [255, 255, 0],
// diffColor: [255, 0, 0],
// diffColorAlt: [0, 0, 255],
// diffMask: false,
},
)
console.log(&#039;pixelmatch2&#039;, result2)
fs.writeFileSync(`${id}.diff2.png`, PNG.sync.write(diff2))
をヘッドレスブラウザで開いて、新しくスクリーンショットを取る
新旧ファイルが揃っているときは、比較する
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


# Page. 30

![Page Image](https://bcdn.docswell.com/page/2EVDXWLYEQ.jpg)

Appendix：ソースコード全録
if (result2 &gt; threshold) {
//
LINE
await sendLineMessage(id, url)
}
//
fs.unlinkSync(`${id}.old.png`)
画像をクラウドにアップロードして、
にメッセージ送信する
ファイル名を変更
}
fs.renameSync(`${id}.new.png`, `${id}.old.png`)
}
const all = async () =&gt; {
await main(
&#039;id1&#039;,
&#039;url1&#039;,
)
await main(
&#039;id2&#039;,
&#039;url2&#039;,
)
await main(&#039;id3&#039;, &#039;url3&#039;, 8)
}
Promise.all([all()])
.then((resutls) =&gt; {
console.log(&#039;OK!&#039;, resutls)
})
.catch((errors) =&gt; {
console.error(&#039;ERROR.&#039;, errors)
exit()
})
.finally(() =&gt; {
console.log(&#039;EXIT&#039;)
exit()
})
Copyright (C) 2022 Toranoana Inc. All Rights Reserved.


