Skip to content

Commit

Permalink
右键菜单使用新的写法
Browse files Browse the repository at this point in the history
  • Loading branch information
maicss committed Oct 12, 2020
1 parent dc570bd commit c2ea138
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 24 deletions.
15 changes: 7 additions & 8 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ chrome.runtime.onMessage.addListener(function (req, sender, sendResponse) {
switch (req.action) {
case 'lookup':
lookUp(req.word)
.then(res => checkWordAdded(res.id).then(existsRes => {res.exists = existsRes.objects[0].exists; return res}).catch(e => {console.error('查询单词是否存在于单词本失败', e); res.exists = 'error'; return res}))
.then(res => checkWordAdded(res.id).then(existsRes => {res.exists = existsRes.objects[0].exists; return res}))
.then(data => chrome.tabs.sendMessage(sender.tab.id, {'action': 'lookup', data}))
.catch(data => chrome.tabs.sendMessage(sender.tab.id, {'action': 'lookup', data}))
break
case 'addOrForget':
addOrForget(req.word, req.wordID).then(res => {
Expand All @@ -40,7 +39,6 @@ chrome.runtime.onMessage.addListener(function (req, sender, sendResponse) {
case 'getWordExample':
getWordExampleSentence(req.id)
.then(data => chrome.tabs.sendMessage(sender.tab.id, {'action': 'getWordExample', data}))
.catch(data => chrome.tabs.sendMessage(sender.tab.id, {'action': 'getWordExample', data}))
break
case 'playSound':
playSound(req.url)
Expand Down Expand Up @@ -103,13 +101,14 @@ chrome.storage.sync.get('__shanbayExtensionSettings', (settings) => {
if (storage.contextLookup) {
debugLogger('info', 'contextMenu added')
chrome.contextMenus.create({
id: Math.random().toString(36),
title: '在扇贝网中查找 %s',
contexts: ['selection'],
onclick: function (info, tab) {
lookUp(info.selectionText).then(res => {
chrome.tabs.sendMessage(tab.id, {'action': 'lookup', data: res})
})
}
})
chrome.contextMenus.onClicked.addListener((info, tab) => {
lookUp(info.selectionText).then(res => {
chrome.tabs.sendMessage(tab.id, {'action': 'lookup', data: res})
})
})
}
})
Expand Down
16 changes: 4 additions & 12 deletions js/const.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @author maicss
* @file some licences file
* @copyright 2017-2019 maicss
* @copyright 2017-2020 maicss
* */

/**
Expand Down Expand Up @@ -41,7 +41,7 @@ const notify = (opt = {title: '人丑多读书', message: '少壮不努力,老
type: 'basic',
title: opt.title,
message: opt.message,
iconUrl: '../images/icon_48.png'
iconUrl: '../images/icon_48.png',
}
let noteID = Math.random().toString(36)
chrome.notifications.create(noteID, options, (notifyID) => {
Expand Down Expand Up @@ -77,7 +77,7 @@ const request = (url, options = {}) => {
if (options.type === 'buffer') return res.arrayBuffer()
return res.json()
} else if (res.status === 401) {
notify({ title: '扇贝网登录信息已过期', message: '请点击本通知或去扇贝网重新登录,否者将不能使用添加单词、右键查词和背单词提醒等功能。' })
notify({ title: '扇贝网登录信息已过期', message: '请点击本通知或去扇贝网重新登录,否者将不能使用添加单词、右键查词和背单词提醒等功能。', url: 'https://web.shanbay.com/web/account/login/' })
debugLogger('error', `[${new Date().toLocaleDateString()}] request failed ${options.method || 'GET'} ${url} ${JSON.stringify(res)}`)
return Promise.reject({status: 401})
} else {
Expand All @@ -93,15 +93,7 @@ const request = (url, options = {}) => {
url: 'https://web.shanbay.com/web/account/login/'
})
} else {
return e.then(error => {
console.error(error)
// notify({
// title: 'oops, 意外发生了',
// message: JSON.stringify(error),
// url: 'https://github.com/maicss/chrome-shanbay-v2/issues'
// })
return Promise.reject(error)
})
return e.then ? e.then(error => Promise.reject(error)) : Promise.reject(e)
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const popover = (res) => {
let contentHtml = `
<div id="shanbay-title">
<span class="word">${data.content}</span>
<!-- <a href="https://www.shanbay.com/bdc/vocabulary/${data.id}" style="float: right;" target="_blank"> 详细</a> -->
<a href="https://web.shanbay.com/wordsweb/#/detail/${data.id}" style="float: right;" target="_blank"> 查看详情 </a>
<div>${assemblyPronunciationStr()}</div>
</div>
<div id="shanbay-content">
Expand Down Expand Up @@ -198,11 +198,11 @@ chrome.runtime.onMessage.addListener(function (res, sender) {
exampleSentenceDiv.innerHTML = res.data.map((item, index) => `<p>${index + 1}, ${item.content_en} <span class="speaker" data-target="${item.audio.us.urls[0]}"></span></p><p> ${item.content_cn}</p>`).join('')
exampleSentenceDiv.className = 'simple-definition'
exampleSentenceSpan.innerHTML = ''
Array.from(exampleSentenceDiv.querySelectorAll('.speaker').forEach(dom => {
Array.from(exampleSentenceDiv.querySelectorAll('.speaker')).forEach(dom => {
dom.addEventListener('click', function () {
chrome.runtime.sendMessage({action: 'playSound', url: this.dataset.target})
})
}))
})
break
}
})
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "扇贝助手v2",
"version": "2.0.0",
"version": "2.0.1",
"author": "maicss, jinntrance",
"manifest_version": 2,
"description": "基于shanbay网页API查询并加入单词到生词本的应用",
Expand Down

0 comments on commit c2ea138

Please sign in to comment.