Skip to content

Commit

Permalink
Refactor gulp script & Chrome background
Browse files Browse the repository at this point in the history
  • Loading branch information
buunguyen committed Nov 15, 2015
1 parent 63e111e commit a01c98f
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 81 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
Binary file modified dist/chrome.crx
Binary file not shown.
Binary file modified dist/chrome.zip
Binary file not shown.
Binary file modified dist/firefox.xpi
Binary file not shown.
Binary file modified dist/opera.nex
Binary file not shown.
92 changes: 46 additions & 46 deletions gulpfile.js → gulpfile.babel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

const gulp = require('gulp')
const path = require('path')
const merge = require('event-stream').merge
Expand All @@ -9,7 +7,7 @@ const $ = require('gulp-load-plugins')()

// Tasks
gulp.task('clean', () => {
return pipe('./tmp', [$.clean()])
return pipe('./tmp', $.clean())
})

gulp.task('build', (cb) => {
Expand All @@ -35,9 +33,12 @@ gulp.task('test', ['build'], (cb) => {
})

gulp.task('styles', () => {
return pipe('./src/styles/octotree.less',
[$.less(), $.autoprefixer({cascade: true})],
'./tmp')
return pipe(
'./src/styles/octotree.less',
$.less(),
$.autoprefixer({cascade: true}),
'./tmp'
)
})

// Chrome
Expand All @@ -52,12 +53,13 @@ gulp.task('chrome:js', ['chrome:template'], () => {
gulp.task('chrome', ['chrome:js'], () => {
return merge(
pipe('./icons/**/*', './tmp/chrome/icons'),
pipe(['./libs/**/*', './tmp/octotree.*', './src/config/chrome/**/*', '!./src/config/chrome/storage.js'], './tmp/chrome/')
pipe(['./libs/**/*', './tmp/octotree.*', './src/config/chrome/manifest.json'], './tmp/chrome/'),
pipe('./src/config/chrome/background.js', $.babel(), './tmp/chrome/')
)
})

gulp.task('chrome:zip', () => {
return pipe('./tmp/chrome/**/*', [$.zip('chrome.zip')], './dist')
return pipe('./tmp/chrome/**/*', $.zip('chrome.zip'), './dist')
})

gulp.task('chrome:_crx', (cb) => {
Expand All @@ -77,24 +79,7 @@ gulp.task('opera', ['chrome'], () => {
})

gulp.task('opera:nex', () => {
return pipe('./dist/chrome.crx', [$.rename('opera.nex')], './dist')
})

// Safari
gulp.task('safari:template', () => {
return buildTemplate({SAFARI: true})
})

gulp.task('safari:js', ['safari:template'], () => {
return buildJs([], {SAFARI: true})
})

gulp.task('safari', ['safari:js'], () => {
return merge(
pipe('./icons/**/*', './tmp/safari/octotree.safariextension/icons'),
pipe(['./libs/**/*', './tmp/octotree.js', './tmp/octotree.css',
'./src/config/safari/**/*', '!./src/config/safari/storage.js'], './tmp/safari/octotree.safariextension/')
)
return pipe('./dist/chrome.crx', $.rename('opera.nex'), './dist')
})

// Firefox
Expand All @@ -109,8 +94,8 @@ gulp.task('firefox:js', ['firefox:template'], () => {
gulp.task('firefox', ['firefox:js'], () => {
return merge(
pipe('./icons/**/*', './tmp/firefox/data/icons'),
pipe(['./libs/**/*', './tmp/octotree.js', './tmp/octotree.css'], './tmp/firefox/data'),
pipe(['./src/config/firefox/firefox.js'], './tmp/firefox/lib'),
pipe(['./libs/**/*', './tmp/octotree.*'], './tmp/firefox/data'),
pipe('./src/config/firefox/firefox.js', $.babel(), './tmp/firefox/lib'),
pipe('./src/config/firefox/package.json', './tmp/firefox')
)
})
Expand All @@ -119,20 +104,31 @@ gulp.task('firefox:xpi', (cb) => {
$.run('cd ./tmp/firefox && cfx xpi --output-file=../../dist/firefox.xpi').exec(cb)
})

// Helpers
function pipe(src, transforms, dest) {
if (typeof transforms === 'string') {
dest = transforms
transforms = null
}
// Safari
gulp.task('safari:template', () => {
return buildTemplate({SAFARI: true})
})

let stream = gulp.src(src)
transforms && transforms.forEach(function (transform) {
stream = stream.pipe(transform)
})
gulp.task('safari:js', ['safari:template'], () => {
return buildJs([], {SAFARI: true})
})

if (dest) stream = stream.pipe(gulp.dest(dest))
return stream
gulp.task('safari', ['safari:js'], () => {
return merge(
pipe('./icons/**/*', './tmp/safari/octotree.safariextension/icons'),
pipe(
['./libs/**/*', './tmp/octotree.*', './src/config/safari/**/*'],
'./tmp/safari/octotree.safariextension/'
)
)
})

// Helpers
function pipe(src, ...transforms) {
return transforms.reduce((stream, transform) => {
const isDest = typeof transform === 'string'
return stream.pipe(isDest ? gulp.dest(transform) : transform)
}, gulp.src(src))
}

function html2js(template) {
Expand Down Expand Up @@ -171,16 +167,20 @@ function buildJs(overrides, ctx) {
].concat(overrides)
.concat('./src/octotree.js')

return pipe(src, [
$.babel({presets: ['es2015']}),
return pipe(
src,
$.babel(),
$.concat('octotree.js'),
$.preprocess({context: ctx}),
], './tmp')
'./tmp'
)
}

function buildTemplate(ctx) {
return pipe('./src/template.html', [
return pipe(
'./src/template.html',
$.preprocess({context: ctx}),
html2js('const TEMPLATE = \'$$\'')
], './tmp')
html2js('const TEMPLATE = \'$$\''),
'./tmp'
)
}
66 changes: 33 additions & 33 deletions src/config/chrome/background.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status !== 'loading') return

chrome.tabs.executeScript(tabId, {
code : 'var injected = window.octotreeInjected; window.octotreeInjected = true; injected;',
runAt : 'document_start'
}, function(res) {
code: 'var injected = window.octotreeInjected; window.octotreeInjected = true; injected;',
runAt: 'document_start'
}, (res) => {
if (chrome.runtime.lastError || // don't continue if error (i.e. page isn't in permission list)
res[0]) // value of `injected` above: don't inject twice
return

var cssFiles = [
const cssFiles = [
'jstree.css',
'octotree.css'
]

var jsFiles = [
const jsFiles = [
'jquery.js',
'jquery-ui.js',
'jquery.pjax.js',
Expand All @@ -24,28 +24,24 @@ chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
]

eachTask([
function(cb) {
eachItem(cssFiles, inject('insertCSS'), cb)
},
function(cb) {
eachItem(jsFiles, inject('executeScript'), cb)
}
(cb) => eachItem(cssFiles, inject('insertCSS'), cb),
(cb) => eachItem(jsFiles, inject('executeScript'), cb)
])

function inject(fn) {
return function(file, cb) {
return (file, cb) => {
chrome.tabs[fn](tabId, { file: file, runAt: 'document_start' }, cb)
}
}
})
})

chrome.runtime.onMessage.addListener(function(req, sender, sendRes) {
var handler = {
requestPermissions: function() {
var urls = (req.urls || [])
.filter(function(url) { return url.trim() !== '' })
.map(function(url) {
chrome.runtime.onMessage.addListener((req, sender, sendRes) => {
const handler = {
requestPermissions: () => {
const urls = (req.urls || [])
.filter((url) => url.trim() !== '')
.map((url) => {
if (url.slice(-2) === '/*') return url
if (url.slice(-1) === '/') return url + '*'
return url + '/*'
Expand All @@ -56,19 +52,26 @@ chrome.runtime.onMessage.addListener(function(req, sender, sendRes) {
removeUnnecessaryPermissions()
}
else {
chrome.permissions.request({ origins: urls }, function(granted) {
chrome.permissions.request({ origins: urls }, (granted) => {
sendRes(granted)
removeUnnecessaryPermissions()
})
}
return true

function removeUnnecessaryPermissions() {
chrome.permissions.getAll(function(permissions) {
var toBeRemovedUrls = permissions.origins.filter(function(url) {
return (url !== 'https://github.com/*' || url !== 'https://gitlab.com/*') && !~urls.indexOf(url)
const whitelist = urls.concat([
'https://github.com/*',
'https://gitlab.com/*'
])
chrome.permissions.getAll((permissions) => {
const toBeRemovedUrls = permissions.origins.filter((url) => {
return !~whitelist.indexOf(url)
})
if (toBeRemovedUrls.length) chrome.permissions.remove({ origins: toBeRemovedUrls })

if (toBeRemovedUrls.length) {
chrome.permissions.remove({ origins: toBeRemovedUrls })
}
})
}
}
Expand All @@ -78,18 +81,15 @@ chrome.runtime.onMessage.addListener(function(req, sender, sendRes) {
})

function eachTask(tasks, done) {
next(0)
function next(index) {
(function next(index = 0) {
if (index === tasks.length) done && done()
else tasks[index](function() { next(index + 1) })
}
else tasks[index](() => next(++index))
})()
}

function eachItem(arr, iter, done) {
var tasks = arr.map(function(item) {
return function(next) {
iter(item, next)
}
const tasks = arr.map((item) => {
return (cb) => iter(item, cb)
})
return eachTask(tasks, done)
}
}
4 changes: 2 additions & 2 deletions src/config/firefox/firefox.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var data = require('sdk/self').data
, pageMod = require('sdk/page-mod')
const data = require('sdk/self').data
const pageMod = require('sdk/page-mod')

pageMod.PageMod({
include: ['https://github.com/*', 'https://gitlab.com/*'],
Expand Down

0 comments on commit a01c98f

Please sign in to comment.