Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dmiyamoto committed Jul 25, 2019
0 parents commit 2fd2f06
Show file tree
Hide file tree
Showing 9 changed files with 890 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output/*
node_modules
.env
45 changes: 45 additions & 0 deletions README.md
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 added input/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions input/sample.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"url","ページ名","ログイン有無"
"https://www.yahoo.co.jp/","TOPページ",""
1 change: 1 addition & 0 deletions input/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"url":"https://www.yahoo.co.jp/","pageName":"TOPページ","login":""}]
63 changes: 63 additions & 0 deletions lib/screenshot.js
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();
});
})();
18 changes: 18 additions & 0 deletions lib/shapeCSV.js
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')
Loading

0 comments on commit 2fd2f06

Please sign in to comment.