Refactor gulp script & Chrome background
This commit is contained in:
parent
63e111e438
commit
a01c98f06a
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,5 +1,3 @@
|
|||
'use strict'
|
||||
|
||||
const gulp = require('gulp')
|
||||
const path = require('path')
|
||||
const merge = require('event-stream').merge
|
||||
|
@ -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) => {
|
||||
|
@ -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
|
||||
|
@ -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) => {
|
||||
|
@ -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
|
||||
|
@ -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')
|
||||
)
|
||||
})
|
||||
|
@ -119,20 +104,31 @@ gulp.task('firefox:xpi', (cb) => {
|
|||
$.run('cd ./tmp/firefox && cfx xpi --output-file=../../dist/firefox.xpi').exec(cb)
|
||||
})
|
||||
|
||||
// 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.*', './src/config/safari/**/*'],
|
||||
'./tmp/safari/octotree.safariextension/'
|
||||
)
|
||||
)
|
||||
})
|
||||
|
||||
// Helpers
|
||||
function pipe(src, transforms, dest) {
|
||||
if (typeof transforms === 'string') {
|
||||
dest = transforms
|
||||
transforms = null
|
||||
}
|
||||
|
||||
let stream = gulp.src(src)
|
||||
transforms && transforms.forEach(function (transform) {
|
||||
stream = stream.pipe(transform)
|
||||
})
|
||||
|
||||
if (dest) stream = stream.pipe(gulp.dest(dest))
|
||||
return stream
|
||||
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) {
|
||||
|
@ -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'
|
||||
)
|
||||
}
|
|
@ -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',
|
||||
|
@ -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 + '/*'
|
||||
|
@ -56,7 +52,7 @@ 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()
|
||||
})
|
||||
|
@ -64,11 +60,18 @@ chrome.runtime.onMessage.addListener(function(req, sender, sendRes) {
|
|||
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 })
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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/*'],
|
||||
|
|
Loading…
Reference in New Issue