Skip to content

Commit

Permalink
fix get project name to post is broken
Browse files Browse the repository at this point in the history
  • Loading branch information
pastak committed Feb 18, 2017
1 parent f0832e4 commit 7ea03ac
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/libs/createScrapboxPage.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import config from '../config'
import thenChrome from 'then-chrome'

export default async ({title, url, body}) => {
const projectUrl = 'https://scrapbox.io/' + await config.projectName()
export default async ({title, url, body, projectName}) => {
const projectUrl = 'https://scrapbox.io/' + projectName
thenChrome.tabs.create({
url: projectUrl + '/' + encodeURIComponent(title) + '?body=' + encodeURIComponent(`[${title} ${url}]\n${body}`)
})
Expand Down
11 changes: 6 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ onMessageListener.add('fetchApi', (message, sender, sendResponse) => {
.then((res) => res.json())
.then(sendResponse)
.catch((e) => {
const xhr = new XMLHttpRequest()
const xhr = new window.XMLHttpRequest()
xhr.withCredentials = true
xhr.open('GET', config.getApiUrl(message.apiType))
xhr.onreadystatechange = () => {
Expand All @@ -21,12 +21,13 @@ onMessageListener.add('fetchApi', (message, sender, sendResponse) => {
xhr.send()
})
})
onMessageListener.add('imageSelected', async (message, sender, sendResponse) => {
onMessageListener.add('createScrapboxPage', async (message, sender, sendResponse) => {
const {text, title, imageUrl, projectName} = message
const tab = (await thenChrome.tabs.query({currentWindow: true, active: true}))[0]
const responseURL = await uploadGyazo(message.imageUrl, tab)
const {text, title} = message
const responseURL = await uploadGyazo(imageUrl, tab)
createScrapboxPage({
title: title,
title,
projectName,
body: `[${responseURL} ${tab.url}]\n${text}`,
url: tab.url
})
Expand Down
11 changes: 10 additions & 1 deletion src/option/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ chrome.runtime.sendMessage(chrome.runtime.id, {
optionElm.textContent = project.displayName
selectElm.appendChild(optionElm)
})
selectElm.value = await config.projectName()
const selectedProject = await config.projectName()
console.log(selectedProject)
if (selectedProject) {
selectElm.value = selectedProject
} else {
const unselectedElm = document.createElement('option')
unselectedElm.selected = true
unselectedElm.textContent = '--Please Select Default Project--'
selectElm.insertBefore(unselectedElm, selectElm.children[0])
}
selectElm.addEventListener('change', (elm) => {
config.projectName(elm.target.value)
.then(() => showMessage('saved'))
Expand Down
5 changes: 3 additions & 2 deletions src/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ document.querySelector('#createButton').addEventListener('click', (e) => {
e.preventDefault()
chrome.runtime.sendMessage(chrome.runtime.id, {
target: 'main',
action: 'imageSelected',
action: 'createScrapboxPage',
projectName: selectElm.value,
imageUrl: document.querySelector('.selected').src,
text: document.querySelector('#scrapboxText').value,
title: document.querySelector('#pageTitle').value
Expand Down Expand Up @@ -60,5 +61,5 @@ chrome.runtime.sendMessage(chrome.runtime.id, {
optionElm.textContent = project.displayName
selectElm.appendChild(optionElm)
})
selectElm.value = await config.projectName()
selectElm.value = (await config.projectName()) || projects[0].name
})
10 changes: 7 additions & 3 deletions test/libs/createScrapboxPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import createScrapboxPage from '../../src/libs/createScrapboxPage'

test.beforeEach((t) => {
chrome.flush()
chrome.storage.sync.get.withArgs('projectName').yields({projectName: 'test'})
})

test('createScrapboxPage.js', async (t) => {
t.plan(2)
const {title, url, body} = {title: 'お試し', url: 'https://example.com/example', body: 'hoge'}
await createScrapboxPage({title, url, body})
const {title, url, body, projectName} = {
title: 'お試し',
url: 'https://example.com/example',
body: 'hoge',
projectName: 'test'
}
await createScrapboxPage({title, url, body, projectName})
t.truthy(chrome.tabs.create.calledOnce)
t.truthy(chrome.tabs.create.withArgs({
url: `https://scrapbox.io/test/${encodeURIComponent('お試し')}?body=${encodeURIComponent(`[${title} ${url}]\n${body}`)}`
Expand Down

0 comments on commit 7ea03ac

Please sign in to comment.