Skip to content

Commit

Permalink
Revise actions.js to#12
Browse files Browse the repository at this point in the history
via https://github.com/polygonplanet/tombloo/blob/74e5cc003a08197d8a5e62beeca5e7eddbe2ca31/tombloo.service.actions.installpatch.fix.js

* Move openInActionBase from actions.js to utility.js
* Update Gruntfile.js

Thanks, polygonplanet!
  • Loading branch information
syoichi committed Jan 8, 2014
1 parent 6169df4 commit beb25e3
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 98 deletions.
5 changes: 2 additions & 3 deletions Gruntfile.js
Expand Up @@ -7,9 +7,8 @@ module.exports = function (grunt) {
all: [
'Gruntfile.js',
// 'xpi/components/tombfix.js',
// 'xpi/chrome/content/library/third_party/MochiKit.js',
// 'xpi/chrome/content/library/component.js',
'xpi/chrome/content/library/prototype.js'
'xpi/chrome/content/library/prototype.js',
// 'xpi/chrome/content/library/utility.js',
// 'xpi/chrome/content/library/database.js',
// 'xpi/chrome/content/library/progress.js',
Expand All @@ -18,7 +17,7 @@ module.exports = function (grunt) {
// 'xpi/chrome/content/library/repository.js',
// 'xpi/chrome/content/library/models.js',
// 'xpi/chrome/content/library/Tombfix.Service.js',
// 'xpi/chrome/content/library/actions.js',
'xpi/chrome/content/library/actions.js'
// 'xpi/chrome/content/library/extractors.js',
// 'xpi/chrome/content/library/ui.js',
// 'xpi/chrome/content/quickPostForm.js',
Expand Down
179 changes: 84 additions & 95 deletions xpi/chrome/content/library/actions.js
@@ -1,96 +1,85 @@
Tombfix.Service.actions = new Repository([
{
type : 'context',
icon : 'chrome://tombfix/skin/tombloo_16.png',
name : getMessage('label.action.installPatch'),
check : function(ctx){
// GitHubでかつraw以外のリンクの場合は除外する
// FIXME: より簡易にインストールできるように
var url = ctx.linkURL;
return ctx.onLink &&
(createURI(url).fileExtension == 'js') &&
!(/github\.com/.test(url) && !/\/raw\//.test(url));
},
execute : function(ctx){
var self = this;

// ファイルタイプを取得しチェックする
var url;
return request(ctx.linkURL).addCallback(function(res){
if(!/^text\/.*(javascript|plain)/.test(res.channel.contentType)){
alert(getMessage('message.install.invalid'));
return;
}

var res = input({
'message.install.warning' : null,
'label.install.agree' : false,
}, 'message.install.warning');
if(!res || !res['label.install.agree'])
return;

return download(ctx.linkURL, getPatchDir()).addCallback(function(file){
// 異常なスクリプトが含まれているとここで停止する
reload();

notify(self.name, getMessage('message.install.success'), notify.ICON_INFO);
});
});
},
},

{
type : 'menu,context',
name : getMessage('label.action.changeAccount'),
execute : function(){
openDialog('chrome://tombfix/content/library/login.xul', 'resizable,centerscreen');
},
},
{
type : 'menu,context',
name : '----',
},
{
type : 'menu,context',
icon : 'chrome://tombfix/skin/tombloo_16.png',
name : getMessage('label.action.tombfixOptions'),
execute : function(){
openDialog('chrome://tombfix/content/prefs.xul', 'resizable,centerscreen');
},
},
]);
/* global request, Tombfix, Repository, getMessage, createURI, alert, input */
/* global download, getPatchDir, reload, notify, openDialog */

var openInActionBase = {
check : function(ctx){
return true;
},
execute : function(ctx){
var app = this.getFile(getPrefValue(this.prefKey));
if(!app){
while(true){
var path = prompt(this.prompt);
if(path === null)
return;

app = this.getFile(path);
if(app){
setPrefValue(this.prefKey, path);
break;
}
}
}

try{
new Process(app).run(false, [ctx.href], 1);
}catch(e){
alert(e);
setPrefValue(this.prefKey, '');
}
},
getFile : function(path){
try{
var file = getLocalFile(path);
return file.exists() && file.isFile() && file;
}catch(e){}
},
}
(function executeActions(global) {
'use strict';

global.Actions = Tombfix.Service.actions = new Repository([
// FIXME: より簡易にインストールできるように
{
type : 'context',
icon : 'chrome://tombfix/skin/tombloo_16.png',
name : getMessage('label.action.installPatch'),
check : function check(ctx) {
if (ctx.onLink) {
let uri = createURI(ctx.linkURL);

return uri.fileExtension === 'js' && ((
// GitHubでかつraw以外のリンクの場合は除外する
/^(?:gist\.)?github\.com$/.test(uri.host) &&
/\/raw\//.test(uri.path)
) || uri.host === 'raw.github.com');
}
},
execute : function execute(ctx) {
// ファイルタイプを取得しチェックする
return request(ctx.linkURL).addCallback(res => {
var result;

if (res.channel.contentType !== 'text/plain') {
alert(getMessage('message.install.invalid'));

return;
}

result = input({
'message.install.warning' : null,
'label.install.agree' : false,
}, 'message.install.warning');

if (!(result && result['label.install.agree'])) {
return;
}

return download(ctx.linkURL, getPatchDir()).addCallback(() => {
// 異常なスクリプトが含まれているとここで停止する
reload();
notify(
this.name,
getMessage('message.install.success'),
notify.ICON_INFO
);
});
});
}
},

{
type : 'menu,context',
name : getMessage('label.action.changeAccount'),
execute : function execute() {
openDialog(
'chrome://tombfix/content/library/login.xul',
'resizable,centerscreen'
);
}
},

{
type : 'menu,context',
name : '----'
},

{
type : 'menu,context',
icon : 'chrome://tombfix/skin/tombloo_16.png',
name : getMessage('label.action.tombfixOptions'),
execute : function execute() {
openDialog(
'chrome://tombfix/content/prefs.xul',
'resizable,centerscreen'
);
}
}
]);
}(this));
41 changes: 41 additions & 0 deletions xpi/chrome/content/library/utility.js
Expand Up @@ -2495,6 +2495,47 @@ AbstractSessionService = {
},
}

var openInActionBase = {
check : function check() {
return true;
},
execute : function execute(ctx) {
var app = this.getFile(getPrefValue(this.prefKey));

if (!app) {
while (true) {
let path = prompt(this.prompt);

if (path === null) {
return;
}

app = this.getFile(path);

if (app) {
setPrefValue(this.prefKey, path);

break;
}
}
}

try {
new Process(app).run(false, [ctx.href], 1);
} catch (err) {
alert(err);
setPrefValue(this.prefKey, '');
}
},
getFile : function getFile(path) {
try {
let file = getLocalFile(path);

return file.exists() && file.isFile() && file;
} catch (err) {}
}
};

function commentToText(commentFunc) {
return commentFunc.toString().replace(/^.*?\r?\n/, '').replace(/\r?\n.*?$/, '');
}
Expand Down

0 comments on commit beb25e3

Please sign in to comment.