Last active
March 18, 2018 07:40
-
-
Save sota1235/dd08f4273f741ac5c841c58f82332511 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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)})})(); |
This file contains hidden or 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
/** | |
* 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