Skip to content
Merged
37 changes: 26 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,16 @@ The codesy.io widget is an add-on for Firefox, Chrome, and Opera that adds codes
2. Install requirements:
* `cd widgets`
* `npm install` (You can install globally with `npm install -g`)
3. Run these gulp tasks to watch changes to files and compile extensions.
* gulp dev-chrome-unpacked - creates chrome/ directory and watches for changes
* gulp dev-chrome-packed - creates a zip file in /chrome and watches for changes
* gulp dev-firefox-unpacked - creates firefox/ directory and watches for changes
* gulp dev-firefox-packed - creates an xpi in firefox/ file and watches for changes

combined tasks:
* gulp dev-packed - creates addon packages and watches for changes
* gulp dev-unpacked - creates directories with addon files and watches all for changes
3. See the list of gulp tasks build or watch changes to files and compile extensions:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With dev tasks removed from the doc, we need to change the lines below that say "Run one of the dev-___ tasks above".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does 382d507 fix this?



### To use the Chrome Extension
1. Run one of the dev-chrome tasks above
1. Run `workon-chrome-directory`
2. Follow the [Unpacked Chrome Extensions
docs](http://developer.chrome.com/extensions/getstarted.html#unpacked) and load the `chrome` directory

### To use the Firefox Add-on
1. Run one of the dev-firefox tasks above
1. Run `gulp workon-firefox-file`
2. Goto the the //about:addons page
3. Select 'Install Addon from File ...')
4. Load the xpi file from the firefox directory
Expand All @@ -45,3 +37,26 @@ combined tasks:
* Removes debug lines from .js files e.g. console.log
* Creates an xpi file for uploading to the moz store. The file contains the manifest.json file.
2. Upload the xpi file to the moz store


## Gulp Tasks

#### build-{browser}-file:
* **build-firefox-file**: create xpi for FF dev in the firefox.source directory with dev settings
* **build-chrome-file**: create zip for chrome dev in the chrome.source directory with dev settings

#### build-{browser}-directory:
* **build-firefox-directory**: create firefox dev directroy in the firefox.source directory with dev settings
* **build-chrome-directory**: create chrome dev directroy in the chrome.source directory with dev settings

#### workon-{browser}-directory or workon-{browser}-directory:
watches extension files and rebuilds
* **workon-firefox-file**
* **workon-firefox-directory**
* **workon-chrome-file**
* **workon-chrome-directory**

#### publish-{browser}-file
* **publish-firefox-file**: create xpi for FF prod
* **publish-chrome-file**: create zip for chrome and opera
* **publish-all**: creates all browser extension files
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I try to run any of these gulp tasks, I get:

Error: Task requires a name that is a string
    at Gulp.Orchestrator.add (/Users/lcrouch/code/codesy/widgets/node_modules/orchestrator/index.js:41:10)
    at Object.<anonymous> (/Users/lcrouch/code/codesy/widgets/gulpfile.js:146:10)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.require (module.js:500:17)
    at require (internal/module.js:20:19)
    at Liftoff.handleArguments (/usr/local/lib/node_modules/gulp/bin/gulp.js:116:3)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d580d3a should fix this.

200 changes: 78 additions & 122 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,67 +41,47 @@ const settings = {
// destination: (optional) path where files will go. If destination is not included,
// the functions will return a stream of files.

javascript_src = function(options) {
return (
function({source, destination}) {
return function() {
console.log(`gather src ${source}/*.js files`)
console.log(`gather src ${settings.source}/*.js files`)

const js_files = gulp.src([`${source}/*.js`, `${settings.source}/*.js`])
.pipe(headerComment(`codesy widget version ${settings.version}`))

if (destination){
console.log(` destination ${destination}/js`);
return js_files.pipe(gulp.dest(`${destination}/js`))
} else {
return js_files
}
}
function javascript_src ({source, destination}) {
return function() {
console.log(`gather src ${source}/*.js files`)
console.log(`gather src ${settings.source}/*.js files`)

const js_files = gulp.src([`${source}/*.js`, `${settings.source}/*.js`])
.pipe(headerComment(`codesy widget version ${settings.version}`))

if (destination){
console.log(` destination ${destination}/js`);
return js_files.pipe(gulp.dest(`${destination}/js`))
} else {
return js_files
}
)(options)
}
}

static_files = function(destination) {
return (
function(destination,{glob, source}) {
return function() {
const static_stream = gulp.src(glob, { base: source, cwd: source })
if (destination){
return static_stream.pipe(gulp.dest( destination ))
} else {
return static_stream
}
}
}
)(destination,settings.static_files)
}
static_files = ({glob, source}) => gulp.src(glob, { base: source, cwd: source });

// this function needs to include dev server details in the options object:
// dev_server: object with domain and port

const manifest = function (options){
return (
function({source, destination}) {
return function() {
const common = gulp.src(`${settings.source}/manifest.json`)
const additions = gulp.src(`${source}/manifest_additions.json`)
manifest_stream = mergeStream(additions, common)
function manifest({source, destination}){
return function() {
const common = gulp.src(`${settings.source}/manifest.json`)
const additions = gulp.src(`${source}/manifest_additions.json`)
manifest_stream = mergeStream(additions, common)
.pipe(mergeJSON('manifest.json'))
.pipe(jeditor(function(json) {
json.version=settings.version
return json
}))
if (destination){
return manifest_stream.pipe(gulp.dest(destination));
} else {
return manifest_stream
}
if (destination){
return manifest_stream.pipe(gulp.dest(destination));
} else {
return manifest_stream
}
})(options)
}
}

const add_dev_server = function (manifest_stream) {
function add_dev_server (manifest_stream) {
({domain, port} = settings.dev_server)
const warning = 'THIS IS NOT the production manifest.',
dev_permission =`https://${domain}:${port}/`,
Expand All @@ -115,43 +95,38 @@ const add_dev_server = function (manifest_stream) {
}))
}

const package = function (options, zipped, for_dev){
return (
function({source, destination: dest, extension: ext}, zipped, for_dev) {
return function() {
console.log(`package source: ${source}`);
let package_name, destination, package_stream;
let static_stream = (new static_files())()
let manifest_stream = (new manifest({source}))()
const js_stream = (new javascript_src( {source} ))()
.pipe(rename( (path)=>path.dirname += "/js" ))

if (for_dev){
manifest_stream = add_dev_server (manifest_stream)
package_name = `${settings.name}-${settings.version}.dev.${ext}`
} else {
js_stream.pipe(stripDebug())
package_name = `${settings.name}-${settings.version}.${ext}`
}
destination = for_dev ? dest : settings.destination
console.log(`package dest: ${destination}`);

package_stream = mergeStream (manifest_stream,js_stream,static_stream)

if (zipped) {
package_stream
.pipe(zip(package_name))
.pipe(gulp.dest(destination))
} else {
package_stream
.pipe(gulp.dest(destination));
}
}
function package ({source, destination: dest, extension: ext}, zipped, for_dev){
return function() {
console.log(`package source: ${source}`);
const package_name = `${settings.name}-${settings.version}${for_dev?'.dev':''}.${ext}`
const destination = for_dev ? dest : settings.destination
console.log(`package dest: ${destination}`);

let static_stream = static_files(settings.static_files)
let manifest_stream = (new manifest({source}))()
const js_stream = (new javascript_src( {source} ))()
.pipe(rename( (path)=>path.dirname += "/js" ))

if (for_dev){
manifest_stream = add_dev_server (manifest_stream)
} else {
js_stream.pipe(stripDebug())
}
)(options, zipped, for_dev)

const package_stream = mergeStream (manifest_stream,js_stream,static_stream)

if (zipped) {
package_stream
.pipe(zip(package_name))
.pipe(gulp.dest(destination))
} else {
package_stream
.pipe(gulp.dest(destination));
}
}
}

const watch_dev = function ({source}, task) {
function watch_src ({source}, task) {
console.log("start watching");
const manifest_files = [`${settings.source}/manifest.json`,`${source}/manifest_additions.json`]
const js_files = [`${source}/*.js`, `${settings.source}/*.js`]
Expand All @@ -160,48 +135,29 @@ const watch_dev = function ({source}, task) {
gulp.watch(js_files, task)
}

// DEV TASKS
gulp.task('dev-chrome-unpacked', ['chrome-unpacked'], function() {
watch_dev(settings.chrome,['chrome-unpacked'])
})

gulp.task('dev-chrome-packed', ['chrome-dev-zip'], function() {
watch_dev(settings.chrome,['chrome-dev-zip'])
})

gulp.task('dev-firefox-unpacked', ['firefox-unpacked'], function() {
watch_dev(settings.firefox,['firefox-unpacked'])
})

gulp.task('dev-firefox-packed', ['firefox-dev-xpi'], function() {
watch_dev(settings.firefox,['firefox-dev-xpi'])
})
const browsers = ['firefox', 'chrome']

for (browser of browsers){
const build_file_task = `build-${browser}-file`
const build_directory_task = `build-${browser}-directory`
const options = settings[browser]

// ADDON BUILDING TASKS
gulp.task(build_file_task, (new package(options, true, true)))
gulp.task(build_directory_task, (new package(options, false, true)))
gulp.task(`publish-${browser}-file`, (new package(options, true, false)))

// WATCH TASKS
gulp.task(`workon-${browser}-directory`, [build_directory_task],
() => watch_src(options, [build_directory_task])
);
gulp.task(`workon-${browser}-file`, [build_file_task],
() => watch_src(options, [build_file_task])
);
}

gulp.task('dev-unpacked',['dev-chrome-unpacked','dev-firefox-unpacked'])
gulp.task('dev-packed',['dev-chrome-packed','dev-firefox-packed'])
const publish_tasks = browsers.map((b)=>`publish-${b}-file`)
gulp.task('publish-all',publish_tasks)

// FF dev must use file
gulp.task('dev-mixed',['dev-chrome-unpacked','dev-firefox-packed'])


// FILE BUILDING TASKS

// create xpi for FF dev in the firefox.source directory with dev settings
gulp.task('firefox-dev-xpi', (new package(settings.firefox, true, true)))

// create firefox dev directroy in the firefox.source directory with dev settings
gulp.task('firefox-unpacked', (new package(settings.firefox, false, true)))

// create zip for chrome dev in the chrome.source directory with dev settings
gulp.task('chrome-dev-zip', (new package(settings.chrome, true, true)))

// create chrome dev directroy in the chrome.source directory with dev settings
gulp.task('chrome-unpacked', (new package(settings.chrome, false, true)))

// create xpi for FF prod
gulp.task('publish-firefox', (new package(settings.firefox, true, false)))

// create zip for chrome and opera
gulp.task('publish-chrome', (new package(settings.chrome, true, false)))

gulp.task('publish-all',['publish-firefox','publish-chrome'])
gulp.task('workon-mixed',['workon-chrome-directory','workon-firefox-file'])
35 changes: 18 additions & 17 deletions src/csp.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
function makeCspAppender (domain='') {
const csp_names = ['CONTENT-SECURITY-POLICY','X-WEBKIT-CSP']
const name_finder = (name) => (csp_name) => csp_name === name.toUpperCase()
const if_csp = (name) => csp_names.find(name_finder(name)) ? true : false

githubFilter = {
urls: ["https://github.com/*"],
types: ["main_frame"]
};
const codesy_types = 'connect-src child-src script-src style-src';
const is_codesy = (type) => codesy_types.indexOf(type) !== -1;
const add_codesy = (accum, word) =>`${accum} ${word} ${is_codesy(word) ? domain : '' }`;
const insert_domain = (csp) => csp.split(' ').reduce(add_codesy,'');

headerOptions = ["responseHeaders", "blocking"]

const makeCspAppender = function(domain='') {
const types = 'connect-src child-src script-src style-src font-src';
const isType = (word) => types.indexOf(word) !== -1;
const addDomain = (accum, word)=>`${accum} ${word} ${isType(word) ? domain : '' }`
const isCSP = function (name) {
name = name.toUpperCase()
return (name === 'CONTENT-SECURITY-POLICY') || (name === 'X-WEBKIT-CSP');
};
return function({responseHeaders: headers}) {
console.time('codesy map headers');
const responseHeaders = headers.map(function({name, value: v}){
value = isCSP(name) ? v.split(' ').reduce(addDomain,'') : v
const responseHeaders = headers.map(function({name, value: original}){
const value = if_csp(name) ? insert_domain(original) : original
return {name,value}
})
console.timeEnd('codesy map headers');
Expand All @@ -27,7 +21,14 @@ const makeCspAppender = function(domain='') {

let codesyAppender = new makeCspAppender()

const setCodesyAppender = function(domain) {
const githubFilter = {
urls: ["https://github.com/*"],
types: ["main_frame"]
};

const headerOptions = ["responseHeaders", "blocking"]

function setCodesyAppender (domain) {
if (chrome.webRequest.onHeadersReceived.hasListener(codesyAppender)) {
chrome.webRequest.onHeadersReceived.removeListener(codesyAppender);
}
Expand Down
Loading