feat:使用background.js做后台
This commit is contained in:
parent
395e35f3fa
commit
fafffe5aec
@ -1,4 +1,9 @@
|
|||||||
global.browser = require('webextension-polyfill');
|
global.browser = require('webextension-polyfill');
|
||||||
|
import httpUtil from './util/httpUtil.js';
|
||||||
|
|
||||||
|
window.envType = 'background';
|
||||||
|
window.token = localStorage.getItem('token');
|
||||||
|
|
||||||
let token = null;
|
let token = null;
|
||||||
let globalPort = null;
|
let globalPort = null;
|
||||||
|
|
||||||
@ -22,12 +27,21 @@ chrome.extension.onConnect.addListener(port => {
|
|||||||
chrome.contextMenus.create(
|
chrome.contextMenus.create(
|
||||||
{
|
{
|
||||||
title: '添加到书签',
|
title: '添加到书签',
|
||||||
onclick: () => {
|
onclick: (info, tab) => {
|
||||||
globalPort.postMessage('点击');
|
console.log(info, tab);
|
||||||
console.log('我被点击了');
|
globalPort.postMessage(createMsg('addBookmark', null));
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
err => {
|
err => {
|
||||||
console.error(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建一个标准命令
|
||||||
|
* @param {*} code code
|
||||||
|
* @param {*} data data
|
||||||
|
*/
|
||||||
|
function createMsg(code, data) {
|
||||||
|
return JSON.stringify({ code, data });
|
||||||
|
}
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
"chrome_style": true
|
"chrome_style": true
|
||||||
},
|
},
|
||||||
"content_scripts": [{
|
"content_scripts": [{
|
||||||
"matches": ["https://bm.tapme.top/*", "http://bookmark.tapme.top/*", "http://localhost:3000/*"],
|
"matches": ["*://*/*"],
|
||||||
"js": ["static/sso.js"]
|
"js": ["static/sso.js"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,15 +1,22 @@
|
|||||||
/**
|
|
||||||
* web页面植入脚本,用于授权等一系列操作。
|
|
||||||
*/
|
|
||||||
|
|
||||||
console.log('注入了页面');
|
console.log('注入了页面');
|
||||||
var port = chrome.extension.connect({ name: 'data' });
|
var port = chrome.extension.connect({ name: 'data' });
|
||||||
|
/**
|
||||||
|
* 接受background传来的消息
|
||||||
|
*/
|
||||||
port.onMessage.addListener(msg => {
|
port.onMessage.addListener(msg => {
|
||||||
console.log('收到消息:' + msg);
|
console.log('收到消息:' + msg);
|
||||||
console.log(window.location);
|
let obj = JSON.parse(msg);
|
||||||
console.log(window.token);
|
switch (obj.code) {
|
||||||
|
case 'addBookmark':
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.error('未知的命令:' + obj.code);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 接收当前注入页面传来的消息
|
||||||
|
*/
|
||||||
window.addEventListener('message', function(event) {
|
window.addEventListener('message', function(event) {
|
||||||
if (event.data.type === undefined) {
|
if (event.data.type === undefined) {
|
||||||
return;
|
return;
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
var baseUri;
|
var baseUri;
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'development') {
|
if (process.env.NODE_ENV === 'development') {
|
||||||
// baseUri = 'http://localhost:3000';
|
baseUri = 'http://localhost:3000';
|
||||||
baseUri = 'https://bm.tapme.top';
|
// baseUri = 'https://bm.tapme.top';
|
||||||
} else {
|
} else {
|
||||||
baseUri = 'https://bm.tapme.top';
|
baseUri = 'https://bm.tapme.top';
|
||||||
}
|
}
|
||||||
|
0
浏览器插件/bookmark-chrome/src/util/const.js
Normal file
0
浏览器插件/bookmark-chrome/src/util/const.js
Normal file
37
浏览器插件/bookmark-chrome/src/util/httpUtil.js
Normal file
37
浏览器插件/bookmark-chrome/src/util/httpUtil.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import config from './config';
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
axios.defaults.timeout = 15000;
|
||||||
|
axios.defaults.baseURL = config.baseUrl;
|
||||||
|
|
||||||
|
axios.interceptors.request.use(
|
||||||
|
function(config) {
|
||||||
|
config.headers['jwt-token'] = window.token;
|
||||||
|
return config;
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
console.error(error);
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
axios.interceptors.response.use(
|
||||||
|
res => {
|
||||||
|
if (res.data.code === -1) {
|
||||||
|
localStorage.removeItem('token');
|
||||||
|
if (window.envType === 'background') {
|
||||||
|
window.open(config.ssoUrl);
|
||||||
|
} else {
|
||||||
|
window.vueInstance.$router.replace('/public/login');
|
||||||
|
}
|
||||||
|
} else if (res.data.code === 1) {
|
||||||
|
return res.data.data;
|
||||||
|
} else {
|
||||||
|
Promise.reject(res);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error => {
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
export default axios;
|
Loading…
x
Reference in New Issue
Block a user