Last active
December 21, 2017 08:23
-
-
Save dlwr/b83021b74bc1db6266c2141f69aee5d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==Taberareloo== | |
// { | |
// "name" : "Scrapbox Model" | |
// , "description" : "Post to scrapbox.io" | |
// , "include" : ["background", "content"] | |
// , "match" : ["*://scrapbox.io/*"] | |
// , "version" : "0.4.3" | |
// , "downloadURL" : "https://scrapbox.io/api/code/rac8/model.scrapbox.tbrl.js_%E3%83%A9%E3%83%B3%E3%83%80%E3%83%A0%E7%89%88/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; | |
var quotes = ps.body.split("\n").map(function(quote, index) { | |
if (index === 0) { | |
title = quote.trim(); | |
} | |
return '> ' + quote; | |
}).join("\n"); | |
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 { | |
promise = request('http://gif-bpm-counter.herokuapp.com/?url=' + encodeURIComponent(ps.itemUrl), { | |
responseType: 'text' | |
}).then(function(res) { | |
if (res.responseText != '0') { | |
title = res.responseText + 'BPM'; | |
} | |
var image = '[' + ps.itemUrl + ']'; | |
var link = ps.page ? '[' + ps.page + ' ' + ps.pageUrl + ']' : ps.pageUrl; | |
return joinText([image, '', link, '', ps.description], '\n\n'); | |
}) | |
} | |
break; | |
} | |
if (!/BPM$/.test(title)) { | |
title = Math.random().toString(36).slice(-8) | |
} | |
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; | |
} | |
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(' ')); | |
} | |
func({ | |
error: null | |
}); | |
}); | |
return; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment