-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2fd2f06
Showing
9 changed files
with
890 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output/* | ||
node_modules | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<h2 align="center">AutoScreenshotTool</h2> | ||
|
||
## ■Node.js version | ||
v10.16.0 | ||
|
||
## ■Build Setup | ||
|
||
``` directory | ||
# Set your own csv file | ||
input/your-own-csv-file.csv | ||
# csv layout sample | ||
"url","ページ名","ログイン有無" | ||
"https://tsukulink.net","TOPページ","" | ||
"https://tsukulink.net/constructions","工事案件検索ページ","" | ||
"https://tsukulink.net/companies/sign_in","loginページ","" | ||
"https://tsukulink.net/companies/sign_in","login後Myページ","有" | ||
"https://tsukulink.net/constructions/pre_new","工事案件登録方法の選択ページ","有" | ||
``` | ||
|
||
``` bash | ||
# Install dependencies | ||
$ npm install | ||
|
||
# Create new .env file | ||
$ touch .env | ||
|
||
# Set .env | ||
$ vi .env | ||
|
||
## Sample for the content of .env | ||
CSV_FILE_NAME=url | ||
LOGIN_PAGE=https://yahoo.co.jp | ||
ACCOUNT=input[name="log"] | ||
ACCOUNT_VALUE=account_info | ||
PASSWORD=input[name="pwd"] | ||
PASSWORD_VALUE=sample_password | ||
LOGIN_BTN=.login-form__btns-item | ||
|
||
# Launch auto screenshot-tool | ||
$ npm run start | ||
``` | ||
|
||
## ■Screenshot files destination | ||
output/ |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"url","ページ名","ログイン有無" | ||
"https://www.yahoo.co.jp/","TOPページ","" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"url":"https://www.yahoo.co.jp/","pageName":"TOPページ","login":""}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require('dotenv').config(); | ||
const puppeteer = require('puppeteer'); | ||
const fs = require('fs'); | ||
const tmpfile = fs.readFileSync(`input/${process.env.CSV_FILE_NAME}.json`,'utf8'); | ||
const urlList = JSON.parse(tmpfile); | ||
|
||
async function scrollToBottom(page, viewportHeight) { | ||
const getScrollHeight = () => { | ||
return Promise.resolve(document.documentElement.scrollHeight) } | ||
|
||
let scrollHeight = await page.evaluate(getScrollHeight) | ||
let currentPosition = 0 | ||
let scrollNumber = 0 | ||
|
||
while (currentPosition < scrollHeight) { | ||
scrollNumber += 1 | ||
const nextPosition = scrollNumber * viewportHeight | ||
await page.evaluate(function (scrollTo) { | ||
return Promise.resolve(window.scrollTo(0, scrollTo)) | ||
}, nextPosition) | ||
await page.waitForNavigation({waitUntil: 'networkidle2', timeout: 5000}) | ||
.catch(e => console.log('timeout exceed. proceed to next operation')); | ||
|
||
currentPosition = nextPosition; | ||
console.log(`scrollNumber: ${scrollNumber}`) | ||
console.log(`currentPosition: ${currentPosition}`) | ||
|
||
scrollHeight = await page.evaluate(getScrollHeight) | ||
console.log(`ScrollHeight ${scrollHeight}`) | ||
} | ||
} | ||
|
||
(() => { | ||
urlList.forEach(async value => { | ||
const viewportHeight = 1200; | ||
const viewportWidth = 1600; | ||
const browser = await puppeteer.launch({headless: true}); | ||
const page = await browser.newPage(); | ||
|
||
page.setViewport({width: viewportWidth, height: viewportHeight}); | ||
|
||
if (value.login) { | ||
await page.goto(`${process.env.LOGIN_PAGE}`); | ||
|
||
// アカウント名とパスワードを入力 | ||
await page.type(`${process.env.ACCOUNT}`, `${process.env.ACCOUNT_VALUE}`); | ||
await page.type(`${process.env.PASSWORD}`, `${process.env.PASSWORD_VALUE}`); | ||
|
||
// ログインボタンをクリック | ||
await page.click(`${process.env.LOGIN_BTN}`); | ||
} | ||
|
||
await page.goto(value.url); | ||
await page.waitForNavigation({waitUntil: 'networkidle2', timeout: 5000}) | ||
.catch(e => console.log('timeout exceed. proceed to next operation')); | ||
|
||
await scrollToBottom(page, viewportHeight); | ||
await page.screenshot({path:`output/${value.pageName}.png`, fullPage: true}); | ||
console.log(`save screenshot: ${value.url}`); | ||
|
||
await browser.close(); | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require('dotenv').config(); | ||
const fs = require('fs'); | ||
let urlList = fs.readFileSync(`input/${process.env.CSV_FILE_NAME}.csv`,'utf8').split("\n"); | ||
|
||
|
||
const data = []; | ||
|
||
for (let i = 1; i < urlList.length - 1; i++) { | ||
const urlRecord = urlList[i].split(','); | ||
data.push({ | ||
url: urlRecord[0].replace(/\"/g,""), | ||
pageName: urlRecord[1].replace(/\"/g,""), | ||
login: urlRecord[2].replace(/\"/g,"") | ||
}) | ||
|
||
} | ||
|
||
fs.writeFileSync(`input/${process.env.CSV_FILE_NAME}.json`,JSON.stringify(data),'utf8') |
Oops, something went wrong.