Skip to content

Instantly share code, notes, and snippets.

@sota1235
Last active March 18, 2018 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sota1235/dd08f4273f741ac5c841c58f82332511 to your computer and use it in GitHub Desktop.
Save sota1235/dd08f4273f741ac5c841c58f82332511 to your computer and use it in GitHub Desktop.
javascript:(()=>{let getLfURLCode=()=>"\n";let hostname="https://scrapbox.io";let apiURI="/api/pages/:projectName/:pageTitle";let pathname=location.pathname;let[projectName,pageTitle]=pathname.substring(1).split("/");let targetURI=apiURI.replace(":projectName",projectName).replace(":pageTitle",pageTitle);let requestURL=targetURI;let request=new Request(requestURL);let myInit={method:"GET",credentials:"same-origin"};let checkContainsKeyword=body=>{return/#テンプレート|\[テンプレート\]/.test(body)};let openNewPage=(title,body)=>{const encodedBody=encodeURIComponent(body);window.open(`${hostname}/${projectName}/${title}?body=${encodedBody}`)};fetch(request,myInit).then(res=>{if(!res.ok){throw res}return res.json()}).then(data=>{let body="";data.lines.shift();data.lines.forEach(({text:text})=>{body+=text+getLfURLCode()});console.log(body);if(!checkContainsKeyword(body)){alert("テンプレートタグがページに存在しません");throw new Error("Target tag not found")}const newPageTitle=prompt("作成したいページ名を入力してください");openNewPage(newPageTitle,body)}).catch(err=>{console.error(err)})})();
/**
* Only supports Chrome.
* @author sota1235
*/
(() => {
let getLfURLCode = () => '\n';
let hostname = 'https://scrapbox.io';
let apiURI = '/api/pages/:projectName/:pageTitle';
let pathname = location.pathname;
let [ projectName, pageTitle ] = pathname.substring(1).split('/');
let targetURI = apiURI
.replace(':projectName', projectName)
.replace(':pageTitle', pageTitle);
let requestURL = targetURI;
let request = new Request(requestURL);
let myInit = {
method : 'GET',
credentials : 'same-origin',
};
let checkContainsKeyword = body => {
return /#テンプレート|\[テンプレート\]/.test(body);
};
let openNewPage = (title, body) => {
const encodedBody = encodeURIComponent(body);
window.open(`${hostname}/${projectName}/${title}?body=${encodedBody}`);
};
fetch(request, myInit)
.then(res => {
if (!res.ok) {
throw res;
}
return res.json();
})
.then(data => {
let body = '';
data.lines.shift(); // Ignore title.
data.lines.forEach(({ text }) => {
body += text + getLfURLCode();
});
if (!checkContainsKeyword(body)) {
alert('テンプレートタグがページに存在しません');
throw new Error('Target tag not found');
}
const newPageTitle = prompt('作成したいページ名を入力してください');
openNewPage(newPageTitle, body);
})
.catch(err => {
console.error(err);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment