Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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