This commit is contained in:
fanxb 2022-04-10 21:43:25 +08:00
parent d251734267
commit 57a6944ec5
7 changed files with 51 additions and 14 deletions

View File

@ -201,7 +201,9 @@ public class BookmarkServiceImpl implements BookmarkService {
bookmark.setUserId(userId);
bookmark.setCreateTime(System.currentTimeMillis());
bookmark.setAddTime(bookmark.getCreateTime());
bookmark.setIcon(getIconBase64(bookmark.getUrl()));
if (bookmark.getIcon() == null) {
bookmark.setIcon(getIconBase64(bookmark.getUrl()));
}
//文件夹和书签都建立搜索key
pinYinService.changeBookmark(bookmark);
bookmarkDao.insertOne(bookmark);

View File

@ -1,6 +1,12 @@
window.envType = 'background';
window.token = localStorage.getItem('token');
axios.defaults.baseURL = 'https://fleyx.com/bookmark/api';
axios.defaults.headers.common['jwt-token'] = window.token;
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.put['Content-Type'] = 'application/json';
let token = null;
let globalPort = null;
@ -13,6 +19,7 @@ chrome.extension.onConnect.addListener(port => {
console.log(msg);
localStorage.setItem('token', msg.data);
window.token = msg.data;
axios.defaults.headers.common['jwt-token'] = window.token;
token = msg.data;
break;
default:
@ -24,15 +31,29 @@ chrome.extension.onConnect.addListener(port => {
chrome.contextMenus.create(
{
title: '添加到书签',
onclick: function (info, tab) {
onclick: async function (info, tab) {
console.log(info, tab);
alert(tab.title);
httpUtil.put('/bookmark', {
let { favIconUrl, title, url } = tab;
let icon = await axios.get(favIconUrl, { responseType: 'arraybuffer' });
console.log(icon);
icon = `data:` + icon.headers['content-type'] + ';base64,' + window.btoa(String.fromCharCode(...new Uint8Array(icon.data)));
let body = {
path: "",
name: title,
url,
type: 0,
path: '',
name: tab.title,
url: tab.url,
});
icon
}
chrome.tabs.sendMessage(tab.id, { code: "addBookmark", body }, res => {
log.info("send to content");
console.log(res);
})
let res = await axios.put("/bookmark", body);
if (res.data.code == -1) {
alert("还未登录,点击拓展按钮进行登录");
} else if (res.data.code == 0) {
alert("系统错误");
}
},
},
() => {
@ -48,3 +69,9 @@ chrome.contextMenus.create(
function createMsg (code, data) {
return JSON.stringify({ code, data });
}
// 接收background发送的消息
chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
console.log(req);
sendResponse("收到");
})

View File

@ -13,7 +13,7 @@
"default_popup": "popup/index.html"
},
"background": {
"scripts": ["static/js/jquery.js", "background.js"]
"scripts": ["static/js/jquery.js", "static/js/axios.min.js", "background.js"]
},
"options_ui": {
"page": "options/index.html",
@ -22,7 +22,7 @@
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["static/js/jquery.js", "static/js/sso.js"]
"js": ["static/js/jquery.js", "static/js/axios.min.js", "static/js/sso.js"]
}
]
}

View File

@ -1 +0,0 @@
document.getElementById('content').innerText = 'Hello world!';

View File

@ -11,8 +11,10 @@
</style>
</head>
<body>
<h1>chrome-plugin-test</h1>
<a href="https://fleyx.com/userSpace/ssoAuth" target="_blank">点击登录</a>
<p id="content"></p>
<script src="popup.js"></script>
<script>
console.log("popup");
</script>
</body>
</html>

View File

@ -17,7 +17,7 @@ port.onMessage.addListener(msg => {
/**
* 接收当前注入页面传来的消息
*/
window.addEventListener('message', function(event) {
window.addEventListener('message', function (event) {
if (event.data.type === undefined) {
return;
}
@ -31,3 +31,10 @@ window.addEventListener('message', function(event) {
console.error('未知的事件', event);
}
});
// 接收background发送的消息
chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
console.log(req);
sendResponse("收到");
})