Skip to content

Instantly share code, notes, and snippets.

@ailispaw
Last active January 6, 2020 13:09
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ailispaw/2f791f1fb49087861e3dce05fae8aedc to your computer and use it in GitHub Desktop.
Save ailispaw/2f791f1fb49087861e3dce05fae8aedc to your computer and use it in GitHub Desktop.
Taberareloo: Scrapbox Model
// ==Taberareloo==
// {
// "name" : "Scrapbox Model"
// , "description" : "Post to scrapbox.io"
// , "include" : ["background", "content"]
// , "match" : ["*://scrapbox.io/*"]
// , "version" : "0.4.4"
// , "downloadURL" : "https://gist.githubusercontent.com/ailispaw/2f791f1fb49087861e3dce05fae8aedc/raw/model.scrapbox.tbrl.js"
// }
// ==/Taberareloo==
(function() {
var BASE_URL = 'https://scrapbox.io';
if (inContext('background')) {
var Scrapbox = {
name : 'Scrapbox',
ICON : BASE_URL + '/assets/img/logo.png',
LINK : BASE_URL + '/',
LOGIN_URL : BASE_URL + '/login/google',
INFO_URL : BASE_URL + '/api/projects/<project-name>',
POST_URL : BASE_URL + '/<project-name>/new',
check : function (ps) {
return /regular|photo|quote|link|video/.test(ps.type);
},
inProject : function () {
var self = this;
return request(BASE_URL + '/api/users/me', {
responseType : 'json'
}).then(function (res) {
var name = res.response.name;
return request(self.INFO_URL, {
responseType : 'json'
}).then(function (res) {
var json = res.response;
for (var i in json.users) {
if (json.users[i].name === name) {
return true;
}
}
throw new Error(self.name + ': you do not join this project.');
}, function (res) {
if (res.status === 403) {
throw new Error(self.name + ': is private. You do not have the permission.');
}
throw new Error(self.name + ': does not exist.');
});
}, function (res) {
throw new Error(chrome.i18n.getMessage('error_notLoggedin', self.name));
});
},
post : function (ps) {
var self = this;
var title;
var promise;
switch (ps.type) {
case 'regular':
if (ps.item) {
title = ps.item;
promise = Promise.resolve(ps.description);
} else {
title = ps.description;
promise = Promise.resolve('');
}
break;
case 'quote':
var link = ps.page ? '[' + ps.page + ' ' + ps.pageUrl + ']' : ps.pageUrl;
if (ps.body.trim()) {
var quotes = ps.body.split("\n").map(function (quote, index) {
if (index === 0) {
title = quote.trim();
}
return '> ' + quote;
}).join("\n");
} else {
title = ps.item || ps.page;
}
promise = Promise.resolve(joinText([quotes, '', link, '', ps.description], '\n\n'));
break;
case 'link':
title = ps.item || ps.description || ps.page;
var link = ps.page ? '[' + ps.page + ' ' + ps.pageUrl + ']' : ps.pageUrl;
promise = Promise.resolve(joinText([link, '', ps.description], '\n\n'));
break;
case 'video':
title = ps.item || ps.description || ps.page;
var link = '[' + ps.pageUrl + ']';
promise = Promise.resolve(joinText([link, '', ps.description], '\n\n'));
break;
case 'photo':
title = ps.item || ps.description || ps.page;
if (ps.file) {
promise = Models['Gyazo'].upload(ps).then(function (url) {
var link = '';
var image = '[' + url + ']';
if (ps.pageUrl) {
link = ps.page ? '[' + ps.page + ' ' + ps.pageUrl + ']' : ps.pageUrl;
} else {
link = url;
}
return joinText([image, '', link, '', ps.description], '\n\n');
});
} else {
var image = '[' + ps.itemUrl + ']';
var link = ps.page ? '[' + ps.page + ' ' + ps.pageUrl + ']' : ps.pageUrl;
promise = Promise.resolve(joinText([image, '', link, '', ps.description], '\n\n'));
}
break;
}
return this.inProject().then(function () {
return promise.then(function (body) {
return new Promise(function (resolve, reject) {
chrome.tabs.create({
url : self.POST_URL,
active : true
}, function (tab) {
setTimeout(function () {
chrome.tabs.sendMessage(tab.id, {
request : 'pasteContextIntoScrapbox',
content : {
title : title,
body : body,
tags : ps.tags
}
}, {frameId: 0}, function (res) {
setTimeout(function () {
chrome.tabs.remove(tab.id, function () {
if (res.error) {
reject(new Error(res.error));
} else {
resolve();
}
});
}, 1000);
});
}, 3000);
});
});
});
});
}
};
Models.scrapboxProjects = [];
Models.getScrapboxProjects = function() {
Models.removeScrapboxProjects();
return request(BASE_URL + '/api/projects', {
responseType : 'json'
}).then(function (res) {
var projects = res.response.projects;
return projects.map(function (project) {
var model = update({}, Scrapbox);
model.name = 'Scrapbox - ' + project.displayName;
model.typeName = 'Scrapbox';
model.INFO_URL = BASE_URL + '/api/projects/' + project.name,
model.POST_URL = BASE_URL + '/' + project.name + '/new',
Models.register(model);
Models.scrapboxProjects.push(model);
return model;
});
}).catch(function (e) {
var message = e.message || e.response.message;
alert('Scrapbox: ' + message);
});
};
Models.removeScrapboxProjects = function() {
Models.scrapboxProjects.forEach(function (model) {
Models.remove(model);
});
Models.scrapboxProjects = [];
};
setTimeout(function() {
Models.getScrapboxProjects();
}, 1000);
return;
}
if (inContext('content')) {
function insertText (text) {
var textarea = document.getElementById('text-input');
textarea.focus();
document.execCommand('insertText', false, text);
}
TBRL.setRequestHandler('pasteContextIntoScrapbox', function (req, sender, func) {
var textarea = document.getElementById('text-input');
if (textarea === null) {
func({error: 'This project does not exist.'});
return;
}
var title = document.title;
insertText(req.content.title.trimTag());
if (req.content.body) {
insertText('\n');
insertText(req.content.body.trimTag());
}
if (Array.isArray(req.content.tags) && (req.content.tags.length > 0)) {
insertText('\n\n');
insertText(req.content.tags.map(function (tag) {
return '#' + tag;
}).join(' '));
}
var isCompleted = function() {
if (title == document.title) {
setTimeout(isCompleted, 1000);
}
setTimeout(function() {
func({error: null});
}, 1000);
};
setTimeout(isCompleted, 1000);
});
return;
}
})();
// ==Taberareloo==
// {
// "name" : "Fix Gyazo.upload 2016.12"
// , "description" : "Fix Gyazo.upload 2016.12"
// , "include" : ["background"]
// , "version" : "0.1.0"
// , "downloadURL" : "https://gist.githubusercontent.com/ailispaw/2f791f1fb49087861e3dce05fae8aedc/raw/patch.fix.gyazo.upload.tbrl.js"
// }
// ==/Taberareloo==
(function() {
addAround(Models['Gyazo'], 'upload', function (proceed, args, target, methodName) {
var self = target;
var ps = args[0];
self.POST_URL = 'https://upload.gyazo.com/upload.cgi';
return self._download(ps).then(function (file) {
return request(self.POST_URL, {
sendContent : {
imagedata : file
}
}).then(function (res) {
return res.responseText;
});
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment