Skip to content
This repository was archived by the owner on Oct 31, 2020. It is now read-only.

Commit be9ad52

Browse files
committed
Browserify: first step
1 parent fb8022e commit be9ad52

File tree

12 files changed

+266
-259
lines changed

12 files changed

+266
-259
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/build
12
/node_modules

.jshintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"supergenpass": true
88
},
99

10+
"node": true,
11+
1012
"noempty": true,
1113
"latedef": true,
1214
"quotmark": "single",

gulpfile.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
/* jshint node: true */
21
var gulp = require('gulp');
2+
var browserify = require('gulp-browserify');
33
var shell = require('gulp-shell');
44
var zip = require('gulp-zip');
55

66
var manifest = require('./manifest.json');
77

8+
gulp.task('build', function(){
9+
gulp.src(['src/events.js']).pipe(browserify()).pipe(gulp.dest('./build/'));
10+
gulp.src(['src/page/script.js']).pipe(browserify()).pipe(gulp.dest('./build/'));
11+
gulp.src(['src/options/options.js']).pipe(browserify()).pipe(gulp.dest('./build/'));
12+
});
13+
814
gulp.task('default', function() {
915
var destination = process.env.BUILD_DESTINATION ||
1016
(process.env.HOME ? process.env.HOME + '/Desktop/' : './');
1117

1218
gulp.src([
1319
'_locales/**/*',
1420
'3rd/**/*',
21+
'build/*',
1522
'img/**/*',
1623
'src/**/*',
1724
'LICENSE',

manifest.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
"scripts": [
1818
"3rd/supergenpass-lib.min.js",
1919
"src/lib/hash.js",
20-
"src/lib/i18n.js",
21-
"src/lib/storage.js",
22-
"src/messages.js",
23-
"src/events.js"
20+
"build/events.js"
2421
],
2522
"persistent": true
2623
},

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"devDependencies": {
1313
"gulp": "^3.6.0",
14+
"gulp-browserify": "^0.5.0",
1415
"gulp-shell": "^0.2.4",
1516
"gulp-zip": "^0.3.2",
1617
"jshint": "^2.5.0"

src/events.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
2-
/* global storage */
1+
var storage = require('./lib/storage.js');
2+
3+
require('./messages.js')(storage);
34

5+
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
46
// Returning true from this function keeps the message channel
57
// open for a response to be sent asynchronously.
68
// https://developer.chrome.com/extensions/runtime#event-onMessage
@@ -25,8 +27,7 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
2527

2628
var tabId = sender.tab && sender.tab.id;
2729
chrome.tabs.executeScript(tabId, {file: '3rd/jquery.min.js', allFrames: true});
28-
chrome.tabs.executeScript(tabId, {file: 'src/lib/i18n.js', allFrames: true});
29-
chrome.tabs.executeScript(tabId, {file: 'src/page/script.js', allFrames: true});
30+
chrome.tabs.executeScript(tabId, {file: 'build/script.js', allFrames: true});
3031
chrome.tabs.insertCSS(tabId, {file: 'src/page/styles.css', allFrames: true});
3132
sendResponse({});
3233
});

src/lib/i18n.js

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,68 @@
11
/**
2-
* Abstract i18n functions.
3-
*
4-
* This file globalizes a single name "i18n".
5-
*
6-
* i18n(message_code, [...args])
7-
* Returns a text value for message_code and args
8-
*
9-
* i18n.html(element, message_code, htmls)
10-
* Fills the element with a text value for message_code, with %n replaced
11-
* by raw html snippets from htmls, returns the element
12-
*
13-
* i18n.page(jQuery)
14-
* Runs using jQuery on a page and populates all data-*-msg values.
15-
* Use data-msg=key, data-value-msg, and data-placeholder-msg.
16-
*/
17-
this.i18n = (function(){
18-
'use strict';
2+
* Abstract i18n functions.
3+
*
4+
* This file globalizes a single name "i18n".
5+
*
6+
* i18n(message_code, [...args])
7+
* Returns a text value for message_code and args
8+
*
9+
* i18n.html(element, message_code, htmls)
10+
* Fills the element with a text value for message_code, with %n replaced
11+
* by raw html snippets from htmls, returns the element
12+
*
13+
* i18n.page(jQuery)
14+
* Runs using jQuery on a page and populates all data-*-msg values.
15+
* Use data-msg=key, data-value-msg, and data-placeholder-msg.
16+
*/
1917

20-
var msg = function(name){
21-
var args = Array.prototype.slice.call(arguments);
22-
args.shift();
23-
return chrome.i18n.getMessage.call(chrome.i18n, name, args) || ('[' + name + ']');
24-
};
18+
'use strict';
2519

26-
var html = function(el, key, htmls){
27-
htmls = htmls || [];
20+
var msg = function(name){
21+
var args = Array.prototype.slice.call(arguments);
22+
args.shift();
23+
return chrome.i18n.getMessage.call(chrome.i18n, name, args) || ('[' + name + ']');
24+
};
2825

29-
var m = el.text(msg(key)).html();
26+
var html = function(el, key, htmls){
27+
htmls = htmls || [];
3028

31-
// Reverse is requires to replace %10 before %1
32-
// Although we replace only a single entry,
33-
// it can be reordered in a messages file.
34-
htmls.map(function(html, index){
35-
return {
36-
index: index + 1,
37-
html: html.trim()
38-
};
39-
}).forEach(function(r){
40-
m = m.replace('%'+r.index, r.html);
41-
});
29+
var m = el.text(msg(key)).html();
4230

43-
return el.html(m);
44-
};
31+
// Reverse is requires to replace %10 before %1
32+
// Although we replace only a single entry,
33+
// it can be reordered in a messages file.
34+
htmls.map(function(html, index){
35+
return {
36+
index: index + 1,
37+
html: html.trim()
38+
};
39+
}).forEach(function(r){
40+
m = m.replace('%'+r.index, r.html);
41+
});
4542

43+
return el.html(m);
44+
};
4645

47-
var api = msg;
4846

49-
api.html = html;
47+
var api = msg;
5048

51-
api.page = function($){
52-
$('[data-msg]').each(function(){
53-
var el = $(this);
54-
var args = el.data('msg').split(';');
55-
var key = args.shift();
56-
html(el, key, args);
57-
});
58-
$('[data-value-msg]').each(function(){
59-
var el = $(this);
60-
el.val(msg(el.data('value-msg')));
61-
});
62-
$('[data-placeholder-msg]').each(function(){
63-
var el = $(this);
64-
el.prop('placeholder', msg(el.data('placeholder-msg')));
65-
});
66-
};
49+
api.html = html;
6750

68-
return api;
69-
})();
51+
api.page = function($){
52+
$('[data-msg]').each(function(){
53+
var el = $(this);
54+
var args = el.data('msg').split(';');
55+
var key = args.shift();
56+
html(el, key, args);
57+
});
58+
$('[data-value-msg]').each(function(){
59+
var el = $(this);
60+
el.val(msg(el.data('value-msg')));
61+
});
62+
$('[data-placeholder-msg]').each(function(){
63+
var el = $(this);
64+
el.prop('placeholder', msg(el.data('placeholder-msg')));
65+
});
66+
};
67+
68+
module.exports = api;

0 commit comments

Comments
 (0)