Skip to content

Commit 278ca01

Browse files
committed
add some gulp task
1 parent 8c7813a commit 278ca01

File tree

13 files changed

+399
-129
lines changed

13 files changed

+399
-129
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cache:
1515
- node_modules
1616

1717
script:
18-
- npm run pack
18+
- npm run crx
1919

2020
notifications:
2121
email:

gulpfile.js

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
const gulp = require('gulp')
24
const path = require('path')
35
const fs = require('fs')
@@ -7,14 +9,19 @@ const tslint = require('tslint')
79
const gTslint = require('gulp-tslint')
810
const clean = require('gulp-clean')
911
const webpack = require('webpack')
12+
const webpackMerge = require('webpack-merge')
1013
const crx = require('gulp-crx-pack')
1114
const zip = require('gulp-zip')
1215

16+
let webpackConfig = require(path.resolve('webpack.config'))
17+
1318
gulp.task('clean', () => gulp.src(['pack/js/', 'dist']).pipe(clean()))
1419

15-
gulp.task('lint', () => gulp.src(['src/**/*.ts'], { base: '.' })
20+
gulp.task('lint', () => gulp.src(['src/**/*.ts'])
1621
.pipe(gTslint({
17-
tslint: tslint,
22+
fix: true,
23+
tslint,
24+
formatter: 'verbose',
1825
program: tslint.Linter.createProgram(path.resolve('tsconfig.json')),
1926
configuration: path.resolve('tslint.json')
2027
}))
@@ -32,20 +39,50 @@ gulp.task('fmt', () => {
3239
// verbose: true,
3340
tsfmt: true,
3441
tsfmtFile: path.resolve('tsfmt.json')
35-
}).then(result => {
42+
}).then((result) => {
3643
if (result.error) console.error(result.message)
3744
cb(null, file)
38-
}, err => cb(err))
45+
}, cb)
3946
})
4047
gulp.src(['src/**/*.ts'], { base: '.' }).pipe(formatting)
4148
})
4249

43-
gulp.task('build', ['clean', 'fmt', 'lint'], done =>
44-
webpack(require(path.resolve('webpack.config.js'))).run((err, stats) => {
50+
gulp.task('build:dev', ['clean', 'fmt', 'lint'], done => {
51+
webpackConfig = webpackMerge(webpackConfig, {
52+
devtool: 'source-map',
53+
plugins: [
54+
new webpack.optimize.CommonsChunkPlugin({
55+
name: 'vendor',
56+
sourceMap: true,
57+
minChunks: Infinity
58+
})
59+
]
60+
})
61+
webpack(webpackConfig).run((err, stats) => {
4562
if (err) console.error(err)
4663
if (done) done()
4764
})
48-
)
65+
})
66+
67+
gulp.task('build', ['clean', 'fmt', 'lint'], done => {
68+
webpackConfig = webpackMerge(webpackConfig, {
69+
plugins: [
70+
new webpack.optimize.UglifyJsPlugin({
71+
sourceMap: false,
72+
comments: false
73+
}),
74+
new webpack.optimize.CommonsChunkPlugin({
75+
name: 'vendor',
76+
sourceMap: false,
77+
minChunks: Infinity
78+
})
79+
]
80+
})
81+
webpack(webpackConfig).run((err, stats) => {
82+
if (err) console.error(err)
83+
if (done) done()
84+
})
85+
})
4986

5087
gulp.task('crx', ['build'], () => gulp.src(path.resolve('pack'))
5188
.pipe(crx({

pack/html/popup.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!doctype html>
1+
<!DOCTYPE html>
22
<html>
33

44
<head>

package-lock.json

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
{
2-
"name": "byc-helper",
2+
"name": "bcy-helper",
33
"author": "wangxufire <wangxufire@gmail.com>",
44
"license": "GPL-3.0",
5-
"main": "gulpfile.js",
5+
"version": "0.0.0",
66
"scripts": {
77
"tsc": "tsc",
88
"tsc:w": "tsc -w",
9+
"fmt": "./node_modules/.bin/gulp fmt",
10+
"lint": "./node_modules/.bin/gulp lint",
11+
"dev": "./node_modules/.bin/gulp build:dev",
912
"build": "./node_modules/.bin/gulp build",
1013
"crx": "./node_modules/.bin/gulp crx",
1114
"pack": "./node_modules/.bin/gulp zip",
1215
"clean": "./node_modules/.bin/gulp clean"
1316
},
1417
"repository": {
1518
"type": "git",
16-
"url": "https://github.com/wangxufire/byc-origin.git"
19+
"url": "https://github.com/acgers/bcy-helper.git"
1720
},
1821
"bugs": {
19-
"url": "https://github.com/wangxufire/byc-origin/issues"
22+
"url": "https://github.com/acgers/bcy-helper/issues"
2023
},
2124
"dependencies": {
2225
"jquery": "~3.2.1"
2326
},
2427
"devDependencies": {
2528
"@types/chrome": "~0.0.47",
2629
"@types/jquery": "~3.2.10",
27-
"ts-loader": "^2.3.2",
28-
"typescript": "^2.4.2",
30+
"typescript": "^2.5",
2931
"tslint": "^5.7.0",
3032
"typescript-formatter": "~6.0.0",
31-
"gulp-tslint": "^8.1.2",
33+
"ts-loader": "^2.3.2",
3234
"webpack": "^3.4.1",
35+
"webpack-merge": "^4.1.0",
3336
"event-stream": "^3.3.4",
3437
"gulp": "^3.9.1",
38+
"gulp-tslint": "^8.1.2",
3539
"gulp-clean": "~0.3.2",
3640
"gulp-crx-pack": "~1.0.1",
3741
"gulp-zip": "~4.0.0"

src/background.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
'use strict'
22

3+
type ImageResponse = { durl: string }
4+
35
chrome.contextMenus.create({
46
id: 'm_pic_bcy_origin',
57
title: chrome.i18n.getMessage('saveOrgBtn'),
68
enabled: true,
79
checked: false,
810
// "all", "page", "frame", "selection", "link", "editable", "image",
911
// "video", "audio", "launcher", "browser_action", or "page_action"
10-
contexts: ['image']
12+
contexts: ['image', 'link']
1113
})
1214

1315
chrome.contextMenus.onClicked.addListener((info, tab) => {
1416
if (info.menuItemId === 'm_pic_bcy_origin') {
15-
chrome.tabs.query({ active: true, currentWindow: true }, result => {
17+
chrome.tabs.query({ active: true, currentWindow: true }, (result) => {
1618
result.map((v, i) => {
17-
chrome.tabs.sendMessage(v.id, { type: 'C_SAVE', info, tab }, response => {
18-
const durl: string = response.durl
19-
chrome.downloads.download({ url: durl }, downloadId => {
20-
if (downloadId === undefined) {
21-
console.error(`download ${durl} failed.`)
22-
}
19+
if (typeof v.id === 'number' && tab !== undefined) {
20+
const message: SaveImageMessage = { type: 'C_SAVE', info, tab }
21+
chrome.tabs.sendMessage(v.id, message, (response: any) => {
22+
const resp = response as ImageResponse
23+
const durl: string = resp.durl
24+
chrome.downloads.download({ url: durl }, (downloadId: number) => {
25+
console.log(`download id: ${downloadId} succeed.`)
26+
})
2327
})
24-
})
28+
}
2529
})
2630
})
2731
}

src/content_script.ts

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,34 @@
11
'use strict'
22

3-
let performFix = (fn: (saveUnAuthPicEnabled: boolean) => Promise<boolean>): void => {
4-
chrome.storage.sync.get('ifSaveUnAuthPic', items => {
5-
fn(items['ifSaveUnAuthPic']).then(saveUnAuthPicEnabled => {
3+
type EnableSaveUnAuthPic = (saveUnAuthPicEnabled: boolean) => Promise<boolean>
4+
type ContextEvent = (e: Event) => boolean
5+
type UrlCheck = (url: string) => boolean
6+
7+
const performFix = (fn: EnableSaveUnAuthPic): void => {
8+
chrome.storage.sync.get('ifSaveUnAuthPic', (items: { [key: string]: any }) => {
9+
fn(items['ifSaveUnAuthPic'] as boolean).then((saveUnAuthPicEnabled: boolean) => {
610
if (saveUnAuthPicEnabled) {
7-
const allowCut = e => {
8-
e.stopImmediatePropagation()
9-
return true
10-
}
11-
const allowCopy = e => {
12-
e.stopImmediatePropagation()
13-
return true
14-
}
15-
const allowPaste = e => {
16-
e.stopImmediatePropagation()
17-
return true
18-
}
19-
const allowContextMenu = e => {
11+
const allowContextMenu: ContextEvent = (e) => {
2012
e.stopImmediatePropagation()
2113
return true
2214
}
23-
const allowDragStart = e => {
15+
const allowMouseDown: ContextEvent = (e) => {
2416
e.stopImmediatePropagation()
2517
return true
2618
}
27-
const allowDrag = e => {
19+
const allowSelectStart: ContextEvent = (e) => {
2820
e.stopImmediatePropagation()
2921
return true
3022
}
31-
const allowDrop = e => {
23+
const allowCut: ContextEvent = (e) => {
3224
e.stopImmediatePropagation()
3325
return true
3426
}
35-
const allowMouseDown = e => {
27+
const allowCopy: ContextEvent = (e) => {
3628
e.stopImmediatePropagation()
3729
return true
3830
}
39-
const allowSelectStart = e => {
31+
const allowPaste: ContextEvent = (e) => {
4032
e.stopImmediatePropagation()
4133
return true
4234
}
@@ -45,24 +37,26 @@ let performFix = (fn: (saveUnAuthPicEnabled: boolean) => Promise<boolean>): void
4537
document.addEventListener('mousedown', allowMouseDown, true)
4638
document.addEventListener('selectstart', allowSelectStart, true)
4739

48-
// document.addEventListener('cut', allowCut, true)
49-
// document.addEventListener('copy', allowCopy, true)
50-
// document.addEventListener('paste', allowPaste, true)
51-
// document.addEventListener('dragstart', allowDragStart, true)
52-
// document.addEventListener('drag', allowDrag, true)
53-
// document.addEventListener('drop', allowDrop, true)
40+
document.addEventListener('cut', allowCut, true)
41+
document.addEventListener('copy', allowCopy, true)
42+
document.addEventListener('paste', allowPaste, true)
5443
}
5544
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
56-
switch (request.type) {
45+
const message = request as Message
46+
switch (message.type) {
5747
case 'C_SAVE':
58-
const selectInfo: chrome.contextMenus.OnClickData = request.info
59-
const imgSrc: string = selectInfo.srcUrl!
60-
console.log(`info: ${JSON.stringify(request.info)}`)
61-
console.log(`tab: ${JSON.stringify(request.tab)}`)
62-
const durl: string = imgSrc.substring(0, imgSrc.lastIndexOf('/'))
63-
console.log(`durl: ${durl}`)
64-
if (isImg(durl)) {
65-
sendResponse({ durl })
48+
const imgmsg: SaveImageMessage = request as SaveImageMessage
49+
const selectInfo = imgmsg.info
50+
const imgSrc: string | undefined = selectInfo.srcUrl
51+
console.log(`info: ${JSON.stringify(imgmsg.info)}`)
52+
console.log(`tab: ${JSON.stringify(imgmsg.tab)}`)
53+
if (typeof imgSrc === 'string') {
54+
const durl: string = imgSrc.substring(0, imgSrc.lastIndexOf('/'))
55+
console.log(`durl: ${durl}`)
56+
if (isImg(durl)) {
57+
const response: ImageResponse = { durl }
58+
sendResponse(response)
59+
}
6660
}
6761
break
6862
default: break
@@ -72,9 +66,9 @@ let performFix = (fn: (saveUnAuthPicEnabled: boolean) => Promise<boolean>): void
7266
})
7367
}
7468

75-
performFix(async saveUnAuthPicEnabled => { return saveUnAuthPicEnabled })
69+
performFix(async (saveUnAuthPicEnabled: boolean) => { return saveUnAuthPicEnabled })
7670

77-
const isImg: (string) => boolean = (imgurl: string) => {
71+
const isImg: UrlCheck = (imgurl: string) => {
7872
const suffix = imgurl.substring(imgurl.lastIndexOf('.'), imgurl.length)
7973
return /\.(gif|jpg|jpeg|png|apng|webp|bmp|tiff|svg|exif|wmf)$/.test(suffix.toLowerCase())
8074
}

src/message.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
interface Message {
2+
type: string
3+
}
4+
5+
interface SaveImageMessage extends Message {
6+
info: chrome.contextMenus.OnClickData
7+
tab: chrome.tabs.Tab
8+
}

src/options.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
import * as $ from 'jquery'
44

5+
interface Settings {
6+
ifSaveUnAuthPic: boolean
7+
}
8+
9+
type UserSettings = (settings: Settings) => Promise<Settings>
10+
511
$(() => {
6-
const restoreOptions = (fn: (settings) => Promise<{ ifSaveUnAuthPic }>): void => {
7-
const ifSaveUnAuthPic = false
8-
chrome.storage.sync.get({ ifSaveUnAuthPic: false }, items => {
9-
fn(items).then(settings => {
12+
const restoreOptions = (fn: UserSettings): void => {
13+
chrome.storage.sync.get(null, (items: { [key: string]: any }) => {
14+
fn(<Settings>items).then((settings: Settings) => {
1015
const { ifSaveUnAuthPic } = settings
1116
const allowSaveUnAuth = <HTMLInputElement>$('#allow-un-auth')[0]
12-
allowSaveUnAuth.checked = !!ifSaveUnAuthPic
17+
allowSaveUnAuth.checked = ifSaveUnAuthPic
1318
})
1419
})
1520
}
@@ -25,7 +30,7 @@ $(() => {
2530
}
2631

2732
// document.addEventListener('DOMContentLoaded', restoreOptions);
28-
restoreOptions(async settings => { return settings })
33+
restoreOptions(async (settings: Settings) => { return settings })
2934

3035
$('#save-un-auth-pic').text(chrome.i18n.getMessage('ifSaveUnAuthPic'))
3136
$('#save').text(chrome.i18n.getMessage('optionsSaveBtn'))

0 commit comments

Comments
 (0)