Skip to content

Commit

Permalink
Change approach: use inline options (still buggy)
Browse files Browse the repository at this point in the history
  • Loading branch information
buunguyen committed Jul 11, 2014
1 parent b7cf13d commit 225586d
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 184 deletions.
92 changes: 57 additions & 35 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
var gulp = require('gulp')
, gutil = require('gulp-util')
, less = require('gulp-less')
, clean = require('gulp-clean')
, concat = require('gulp-concat')
, merge = require('event-stream').merge
, series = require('stream-series')
, map = require('map-stream')
, prefix = require('gulp-autoprefixer')
, rseq = require('gulp-run-sequence')
, $ = require('gulp-load-plugins')()

function pipe(src, transforms, dest) {
if (typeof transforms === 'string') {
Expand All @@ -26,7 +21,7 @@ function html2js(template) {
return map(escape)

function escape(file, cb) {
var path = gutil.replaceExtension(file.path, '.js')
var path = $.util.replaceExtension(file.path, '.js')
, content = file.contents.toString()
, escaped = content.replace(/\\/g, "\\\\")
.replace(/'/g, "\\'")
Expand All @@ -38,19 +33,7 @@ function html2js(template) {
}
}

gulp.task('clean', function() {
return pipe('./tmp', [clean()])
})

gulp.task('css', function() {
return pipe('./src/octotree.less', [less(), prefix({ cascade: true })], './tmp')
})

gulp.task('template', function() {
return pipe('./src/template.html', [html2js('const TEMPLATE = \'$$\'')], './tmp')
})

function buildMainScript(additions) {
function buildJs(additions, ctx) {
var src = additions.concat([
'./src/adapter.github.js',
'./src/view.help.js',
Expand All @@ -63,47 +46,86 @@ function buildMainScript(additions) {
'./src/constants.js',
'./src/octotree.js',
])
return pipe(src, [concat('octotree.js')], './tmp')
return pipe(src, [
$.concat('octotree.js'),
$.preprocess({ context: ctx })
], './tmp')
}

function buildTemplate(ctx) {
return pipe('./src/template.html', [
$.preprocess({ context: ctx }),
html2js('const TEMPLATE = \'$$\'')
], './tmp')
}

gulp.task('chrome:octotree', function() {
return buildMainScript(['./src/chrome/storage.js'])
gulp.task('clean', function() {
return pipe('./tmp', [$.clean()])
})

gulp.task('css', function() {
return pipe('./src/octotree.less', [$.less(), $.autoprefixer({ cascade: true })], './tmp')
})
gulp.task('chrome', ['chrome:octotree'], function() {


gulp.task('default', function(cb) {
$.runSequence('clean', 'css', 'chrome', 'opera', 'safari', 'firefox', cb)
gulp.watch(['./src/**/*'], ['default'])
})


// Chrome
gulp.task('chrome:template', function() {
return buildTemplate({ CHROME: true })
})

gulp.task('chrome:js', ['chrome:template'], function() {
return buildJs(['./src/chrome/storage.js'], { CHROME: true })
})

gulp.task('chrome', ['chrome:js'], function() {
return merge(
pipe('./icons/**/*', './tmp/chrome/icons'),
pipe(['./libs/**/*', './tmp/octotree.*', './src/chrome/**/*', '!./src/chrome/storage.js'], './tmp/chrome/')
)
})

// Opera
gulp.task('opera', ['chrome'], function() {
return pipe('./tmp/chrome/**/*', './tmp/opera')
})

gulp.task('safari:octotree', function() {
return buildMainScript(['./src/safari/storage.js'])
// Safari
gulp.task('safari:template', function() {
return buildTemplate({ SAFARI: true })
})

gulp.task('safari:js', ['safari:template'], function() {
return buildJs(['./src/safari/storage.js'], { SAFARI: true })
})
gulp.task('safari', ['safari:octotree'], function() {

gulp.task('safari', ['safari:js'], function() {
return merge(
pipe('./icons/**/*', './tmp/safari/octotree.safariextension/icons'),
pipe(['./libs/**/*', './tmp/octotree.js', './tmp/octotree.css',
'./src/safari/**/*', '!./src/safari/storage.js'], './tmp/safari/octotree.safariextension/')
)
})

gulp.task('firefox:octotree', function() {
return buildMainScript(['./src/firefox/storage.js'])
// Firefox
gulp.task('firefox:template', function() {
return buildTemplate({ FIREFOX: true })
})

gulp.task('firefox:js', ['firefox:template'], function() {
return buildJs(['./src/firefox/storage.js'], { FIREFOX: true })
})
gulp.task('firefox', ['firefox:octotree'], function() {

gulp.task('firefox', ['firefox:js'], function() {
return merge(
pipe('./icons/**/*', './tmp/firefox/data/icons'),
pipe(['./libs/**/*', './tmp/octotree.js', './tmp/octotree.css'], './tmp/firefox/data'),
pipe(['./src/firefox/firefox.js'], './tmp/firefox/lib'),
pipe('./src/firefox/package.json', './tmp/firefox')
)
})

gulp.task('default', function(cb) {
rseq('clean', ['css', 'template'], 'chrome', 'opera', 'safari', 'firefox', cb)
gulp.watch(['./src/**/*'], ['default'])
})
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"map-stream": "*",
"gulp-util": "^2.2.20",
"stream-series": "*",
"gulp-concat": "*"
"gulp-concat": "*",
"gulp-preprocess": "^1.1.1",
"gulp-load-plugins": "^0.5.3"
}
}
87 changes: 58 additions & 29 deletions src/chrome/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,69 @@ chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
})

chrome.runtime.onMessage.addListener(function(req) {
var loaded = req.loaded
, tabId = req.tabId
if (req.type === 'perms') {
var urls = req.urls
if (urls.length === 0) { // TODO: remove only

if (loaded) return
}
else {
console.log(1, urls)
urls = urls.map(function(url) {
if (url.slice(-1) !== '/') url += '/'
url += '*'
return url
})
console.log(2, urls)
chrome.permissions.request({ origins: urls }, function() {
chrome.permissions.getAll(function(permissions) {
var allUrls = permissions.origins
, toRemove = []
console.log('all', allUrls)
allUrls.forEach(function(url) {
if (url !== 'https://github.com/*' && !~urls.indexOf(url)) toRemove.push(url)
})
console.log('remove: ' + toRemove)
if (toRemove.length) chrome.permissions.remove({ origins: toRemove })
})
})
}
}
else {
var loaded = req.loaded
, tabId = req.tabId

var cssFiles = [
'jstree.css',
'octotree.css'
]
if (loaded) return

var jsFiles = [
'jquery.js',
'jquery-ui.js',
'jquery.pjax.js',
'jstree.js',
'keymaster.js',
'async.js',
'octotree.js'
]
var cssFiles = [
'jstree.css',
'octotree.css'
]

async.series([
function(cb) {
async.eachSeries(cssFiles, inject('insertCSS'), cb)
},
function(cb) {
async.eachSeries(jsFiles, inject('executeScript'), cb)
}
], function() {
chrome.tabs.executeScript(tabId, { code: 'var octotreeLoaded = true', runAt: 'document_start' })
})
var jsFiles = [
'jquery.js',
'jquery-ui.js',
'jquery.pjax.js',
'jstree.js',
'keymaster.js',
'async.js',
'octotree.js'
]

async.series([
function(cb) {
async.eachSeries(cssFiles, inject('insertCSS'), cb)
},
function(cb) {
async.eachSeries(jsFiles, inject('executeScript'), cb)
}
], function() {
chrome.tabs.executeScript(tabId, { code: 'var octotreeLoaded = true', runAt: 'document_start' })
})

function inject(fn) {
return function(file, cb) {
chrome.tabs[fn](tabId, { file: file, runAt: 'document_start' }, function() { cb() })
function inject(fn) {
return function(file, cb) {
chrome.tabs[fn](tabId, { file: file, runAt: 'document_start' }, function() { cb() })
}
}
}
})
28 changes: 0 additions & 28 deletions src/chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,5 @@
"background.js"
],
"persistent": true
},
// "content_scripts": [
// {
// "matches": [
// "https://github.com/*"
// ],
// "css": [
// "jstree.css",
// "octotree.css"
// ],
// "js": [
// "jquery.js",
// "jquery-ui.js",
// "jquery.pjax.js",
// "jstree.js",
// "keymaster.js",
// "octotree.js"
// ],
// "run_at": "document_start"
// }
// ],
"browser_action": {
"default_icon": {
"19": "icons/icon19.png",
"38": "icons/icon38.png"
},
"default_title": "Octotree",
"default_popup": "popup.html"
}
}
14 changes: 0 additions & 14 deletions src/chrome/popup.css

This file was deleted.

14 changes: 0 additions & 14 deletions src/chrome/popup.html

This file was deleted.

63 changes: 0 additions & 63 deletions src/chrome/popup.js

This file was deleted.

Loading

0 comments on commit 225586d

Please sign in to comment.