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 gulp = require('gulp')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const merge = require('event-stream').merge
|
const merge = require('event-stream').merge
|
||||||
|
@ -9,7 +7,7 @@ const $ = require('gulp-load-plugins')()
|
||||||
|
|
||||||
// Tasks
|
// Tasks
|
||||||
gulp.task('clean', () => {
|
gulp.task('clean', () => {
|
||||||
return pipe('./tmp', [$.clean()])
|
return pipe('./tmp', $.clean())
|
||||||
})
|
})
|
||||||
|
|
||||||
gulp.task('build', (cb) => {
|
gulp.task('build', (cb) => {
|
||||||
|
@ -35,9 +33,12 @@ gulp.task('test', ['build'], (cb) => {
|
||||||
})
|
})
|
||||||
|
|
||||||
gulp.task('styles', () => {
|
gulp.task('styles', () => {
|
||||||
return pipe('./src/styles/octotree.less',
|
return pipe(
|
||||||
[$.less(), $.autoprefixer({cascade: true})],
|
'./src/styles/octotree.less',
|
||||||
'./tmp')
|
$.less(),
|
||||||
|
$.autoprefixer({cascade: true}),
|
||||||
|
'./tmp'
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Chrome
|
// Chrome
|
||||||
|
@ -52,12 +53,13 @@ gulp.task('chrome:js', ['chrome:template'], () => {
|
||||||
gulp.task('chrome', ['chrome:js'], () => {
|
gulp.task('chrome', ['chrome:js'], () => {
|
||||||
return merge(
|
return merge(
|
||||||
pipe('./icons/**/*', './tmp/chrome/icons'),
|
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', () => {
|
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) => {
|
gulp.task('chrome:_crx', (cb) => {
|
||||||
|
@ -77,24 +79,7 @@ gulp.task('opera', ['chrome'], () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
gulp.task('opera:nex', () => {
|
gulp.task('opera:nex', () => {
|
||||||
return pipe('./dist/chrome.crx', [$.rename('opera.nex')], './dist')
|
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/')
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Firefox
|
// Firefox
|
||||||
|
@ -109,8 +94,8 @@ gulp.task('firefox:js', ['firefox:template'], () => {
|
||||||
gulp.task('firefox', ['firefox:js'], () => {
|
gulp.task('firefox', ['firefox:js'], () => {
|
||||||
return merge(
|
return merge(
|
||||||
pipe('./icons/**/*', './tmp/firefox/data/icons'),
|
pipe('./icons/**/*', './tmp/firefox/data/icons'),
|
||||||
pipe(['./libs/**/*', './tmp/octotree.js', './tmp/octotree.css'], './tmp/firefox/data'),
|
pipe(['./libs/**/*', './tmp/octotree.*'], './tmp/firefox/data'),
|
||||||
pipe(['./src/config/firefox/firefox.js'], './tmp/firefox/lib'),
|
pipe('./src/config/firefox/firefox.js', $.babel(), './tmp/firefox/lib'),
|
||||||
pipe('./src/config/firefox/package.json', './tmp/firefox')
|
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)
|
$.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
|
// Helpers
|
||||||
function pipe(src, transforms, dest) {
|
function pipe(src, ...transforms) {
|
||||||
if (typeof transforms === 'string') {
|
return transforms.reduce((stream, transform) => {
|
||||||
dest = transforms
|
const isDest = typeof transform === 'string'
|
||||||
transforms = null
|
return stream.pipe(isDest ? gulp.dest(transform) : transform)
|
||||||
}
|
}, gulp.src(src))
|
||||||
|
|
||||||
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 html2js(template) {
|
function html2js(template) {
|
||||||
|
@ -171,16 +167,20 @@ function buildJs(overrides, ctx) {
|
||||||
].concat(overrides)
|
].concat(overrides)
|
||||||
.concat('./src/octotree.js')
|
.concat('./src/octotree.js')
|
||||||
|
|
||||||
return pipe(src, [
|
return pipe(
|
||||||
$.babel({presets: ['es2015']}),
|
src,
|
||||||
|
$.babel(),
|
||||||
$.concat('octotree.js'),
|
$.concat('octotree.js'),
|
||||||
$.preprocess({context: ctx}),
|
$.preprocess({context: ctx}),
|
||||||
], './tmp')
|
'./tmp'
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildTemplate(ctx) {
|
function buildTemplate(ctx) {
|
||||||
return pipe('./src/template.html', [
|
return pipe(
|
||||||
|
'./src/template.html',
|
||||||
$.preprocess({context: ctx}),
|
$.preprocess({context: ctx}),
|
||||||
html2js('const TEMPLATE = \'$$\'')
|
html2js('const TEMPLATE = \'$$\''),
|
||||||
], './tmp')
|
'./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
|
if (changeInfo.status !== 'loading') return
|
||||||
|
|
||||||
chrome.tabs.executeScript(tabId, {
|
chrome.tabs.executeScript(tabId, {
|
||||||
code : 'var injected = window.octotreeInjected; window.octotreeInjected = true; injected;',
|
code: 'var injected = window.octotreeInjected; window.octotreeInjected = true; injected;',
|
||||||
runAt : 'document_start'
|
runAt: 'document_start'
|
||||||
}, function(res) {
|
}, (res) => {
|
||||||
if (chrome.runtime.lastError || // don't continue if error (i.e. page isn't in permission list)
|
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
|
res[0]) // value of `injected` above: don't inject twice
|
||||||
return
|
return
|
||||||
|
|
||||||
var cssFiles = [
|
const cssFiles = [
|
||||||
'jstree.css',
|
'jstree.css',
|
||||||
'octotree.css'
|
'octotree.css'
|
||||||
]
|
]
|
||||||
|
|
||||||
var jsFiles = [
|
const jsFiles = [
|
||||||
'jquery.js',
|
'jquery.js',
|
||||||
'jquery-ui.js',
|
'jquery-ui.js',
|
||||||
'jquery.pjax.js',
|
'jquery.pjax.js',
|
||||||
|
@ -24,28 +24,24 @@ chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
|
||||||
]
|
]
|
||||||
|
|
||||||
eachTask([
|
eachTask([
|
||||||
function(cb) {
|
(cb) => eachItem(cssFiles, inject('insertCSS'), cb),
|
||||||
eachItem(cssFiles, inject('insertCSS'), cb)
|
(cb) => eachItem(jsFiles, inject('executeScript'), cb)
|
||||||
},
|
|
||||||
function(cb) {
|
|
||||||
eachItem(jsFiles, inject('executeScript'), cb)
|
|
||||||
}
|
|
||||||
])
|
])
|
||||||
|
|
||||||
function inject(fn) {
|
function inject(fn) {
|
||||||
return function(file, cb) {
|
return (file, cb) => {
|
||||||
chrome.tabs[fn](tabId, { file: file, runAt: 'document_start' }, cb)
|
chrome.tabs[fn](tabId, { file: file, runAt: 'document_start' }, cb)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener(function(req, sender, sendRes) {
|
chrome.runtime.onMessage.addListener((req, sender, sendRes) => {
|
||||||
var handler = {
|
const handler = {
|
||||||
requestPermissions: function() {
|
requestPermissions: () => {
|
||||||
var urls = (req.urls || [])
|
const urls = (req.urls || [])
|
||||||
.filter(function(url) { return url.trim() !== '' })
|
.filter((url) => url.trim() !== '')
|
||||||
.map(function(url) {
|
.map((url) => {
|
||||||
if (url.slice(-2) === '/*') return url
|
if (url.slice(-2) === '/*') return url
|
||||||
if (url.slice(-1) === '/') return url + '*'
|
if (url.slice(-1) === '/') return url + '*'
|
||||||
return url + '/*'
|
return url + '/*'
|
||||||
|
@ -56,7 +52,7 @@ chrome.runtime.onMessage.addListener(function(req, sender, sendRes) {
|
||||||
removeUnnecessaryPermissions()
|
removeUnnecessaryPermissions()
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
chrome.permissions.request({ origins: urls }, function(granted) {
|
chrome.permissions.request({ origins: urls }, (granted) => {
|
||||||
sendRes(granted)
|
sendRes(granted)
|
||||||
removeUnnecessaryPermissions()
|
removeUnnecessaryPermissions()
|
||||||
})
|
})
|
||||||
|
@ -64,11 +60,18 @@ chrome.runtime.onMessage.addListener(function(req, sender, sendRes) {
|
||||||
return true
|
return true
|
||||||
|
|
||||||
function removeUnnecessaryPermissions() {
|
function removeUnnecessaryPermissions() {
|
||||||
chrome.permissions.getAll(function(permissions) {
|
const whitelist = urls.concat([
|
||||||
var toBeRemovedUrls = permissions.origins.filter(function(url) {
|
'https://github.com/*',
|
||||||
return (url !== 'https://github.com/*' || url !== 'https://gitlab.com/*') && !~urls.indexOf(url)
|
'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) {
|
function eachTask(tasks, done) {
|
||||||
next(0)
|
(function next(index = 0) {
|
||||||
function next(index) {
|
|
||||||
if (index === tasks.length) done && done()
|
if (index === tasks.length) done && done()
|
||||||
else tasks[index](function() { next(index + 1) })
|
else tasks[index](() => next(++index))
|
||||||
}
|
})()
|
||||||
}
|
}
|
||||||
|
|
||||||
function eachItem(arr, iter, done) {
|
function eachItem(arr, iter, done) {
|
||||||
var tasks = arr.map(function(item) {
|
const tasks = arr.map((item) => {
|
||||||
return function(next) {
|
return (cb) => iter(item, cb)
|
||||||
iter(item, next)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
return eachTask(tasks, done)
|
return eachTask(tasks, done)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
var data = require('sdk/self').data
|
const data = require('sdk/self').data
|
||||||
, pageMod = require('sdk/page-mod')
|
const pageMod = require('sdk/page-mod')
|
||||||
|
|
||||||
pageMod.PageMod({
|
pageMod.PageMod({
|
||||||
include: ['https://github.com/*', 'https://gitlab.com/*'],
|
include: ['https://github.com/*', 'https://gitlab.com/*'],
|
||||||
|
|
Loading…
Reference in New Issue