feat: 百度统计,转移config目录,防止更新后被删除

pull/180/head
xiaojunnuo 2020-11-12 17:59:34 +08:00
parent 87ac9173f9
commit 274ce7968c
9 changed files with 419 additions and 18355 deletions

View File

@ -1,7 +1,7 @@
# dev-sidecar
开发者边车命名取自service-mesh的service-sidecar意为为开发者打辅助的边车工具
通过本地代理的方式将http请求代理到一些国内的加速通道上
不用翻墙也能解决一些网站和库无法访问或访问速度慢的问题
不用`ss小飞机`也能解决一些网站和库无法访问或访问速度慢的问题

File diff suppressed because it is too large Load Diff

View File

@ -18,6 +18,7 @@
"@docmirror/mitmproxy": "1.0.2",
"ant-design-vue": "^1.6.5",
"core-js": "^3.6.5",
"electron-baidu-tongji": "^1.0.5",
"electron-store": "^6.0.1",
"electron-updater": "^4.3.5",
"es-abstract": "^1.17.7",
@ -29,6 +30,10 @@
"vue-json-editor": "^1.4.2",
"vue-router": "^3.4.8"
},
"resolutions": {
"hexoid": "^1.0.0",
"formidable": "2.0.0-canary.20200504.1"
},
"devDependencies": {
"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-eslint": "~4.5.0",

View File

@ -1,10 +1,12 @@
'use strict'
/* global __static */
import path from 'path'
import { app, protocol, BrowserWindow, Menu, Tray } from 'electron'
import { app, protocol, BrowserWindow, Menu, Tray, ipcMain } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import bridge from './bridge/index'
import updateHandle from './bridge/update-handle'
import { ebtMain } from './tongji'
// eslint-disable-next-line no-unused-vars
const isMac = process.platform === 'darwin'
@ -190,6 +192,10 @@ if (!isFirstInstance) {
}
// 自动更新
updateHandle(win, updateUrl)
// 百度分析
ebtMain(ipcMain, isDevelopment)
try {
// 最小化到托盘
tray = setTray(app)

View File

@ -120,14 +120,14 @@ function _getSettingsPath () {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
return dir + 'setting.json5'
return dir + '/setting.json5'
}
function _getConfigPath () {
const dir = getDefaultConfigBasePath()
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
return dir + 'config.json5'
return dir + '/config.json5'
}
function doMerge (defObj, newObj) {

View File

@ -7,6 +7,10 @@ import VueRouter from 'vue-router'
import routes from './view/router'
import DsContainer from './view/components/container'
import './view/style/index.scss'
import { ipcRenderer } from 'electron'
const tongji = require('./tongji')
Vue.config.productionTip = false
Vue.use(antd)
Vue.use(VueRouter)
@ -25,6 +29,10 @@ view.initApi().then(async (api) => {
render: h => h(App)
}).$mount('#app')
const BAIDU_SITE_ID = 'f2d170ce560aef0005b689f28697f852'
// 百度统计
tongji.ebtRenderer(ipcRenderer, BAIDU_SITE_ID, router)
view.initModules(app)
})

103
packages/gui/src/tongji.js Normal file
View File

@ -0,0 +1,103 @@
const request = require('request')
/**
* first step
* @param {*} ipcMain
*/
const ebtMain = (ipcMain, isDevelopment) => {
/* istanbul ignore else */
if (!(ipcMain && ipcMain.on)) {
throw new TypeError('require ipcMain')
}
// step 2
ipcMain.on('electron-baidu-tongji-message', (event, arg) => {
// electron 生产模式下是直接请求文件系统,没有 http 地址
// 前台拿不到 hm.js 的内容
request({
url: `https://hm.baidu.com/hm.js?${arg}`,
method: 'GET',
headers: {
Referer: 'https://hm.baidu.com/'
}
},
(err, response, body) => {
if (err) {
console.error('百度统计请求出错', err)
return
}
const rource = '(h.c.b.su=h.c.b.u||document.location.href),h.c.b.u=f.protocol+"//"+document.location.host+'
/* istanbul ignore else */
if (body && body.indexOf(rource) >= 0) {
// step 3
let text = body
/* istanbul ignore else */
if (!isDevelopment) {
// 百度统计可能改规则了,不统计 file:// 开始的请求
// 这里强制替换为 https
const target = '(h.c.b.su=h.c.b.u||"https://"+c.dm[0]+a[1]),h.c.b.u="https://"+c.dm[0]+'
const target2 = '"https://"+c.dm[0]+window.location.pathname+window.location.hash'
text = body.replace(rource, target).replace(/window.location.href/g, target2)
}
console.log('baidu tonji: ret')
event.sender.send('electron-baidu-tongji-reply', { text, isDevelopment })
}
})
})
}
/**
* second step
* @param {*} ipcRenderer
* @param {*} siteId
* @param {*} router
*/
const ebtRenderer = (ipcRenderer, siteId, router) => {
/* istanbul ignore else */
if (!(ipcRenderer && ipcRenderer.on && ipcRenderer.send)) {
throw new TypeError('require ipcRenderer')
}
/* istanbul ignore else */
if (!(siteId && typeof siteId === 'string')) {
throw new TypeError('require siteId')
}
// step 4
ipcRenderer.on('electron-baidu-tongji-reply', (_, { text, isDevelopment }) => {
console.log('electron-baidu-tongji-reply')
/* istanbul ignore else */
if (isDevelopment) { document.body.classList.add('electron-baidu-tongji_dev') }
window._hmt = window._hmt || []
const hm = document.createElement('script')
hm.text = text
const head = document.getElementsByTagName('head')[0]
head.appendChild(hm)
// Vue单页应用时监听router的每次变化
// 把虚拟的url地址赋给百度统计的API接口
/* istanbul ignore else */
if (router && router.beforeEach) {
router.beforeEach((to, _, next) => {
/* istanbul ignore else */
if (to.path) {
window._hmt.push(['_trackPageview', '/#' + to.fullPath])
console.log('baidu trace', to.fullPath)
}
next()
})
}
})
// step 1
ipcRenderer.send('electron-baidu-tongji-message', siteId)
}
module.exports = { ebtMain, ebtRenderer }

View File

@ -1,4 +1,6 @@
const path = require('path')
const webpack = require('webpack')
module.exports = {
pages: {
index: {
@ -8,6 +10,9 @@ module.exports = {
},
configureWebpack: config => {
const configNew = {
plugins: [
new webpack.DefinePlugin({ 'global.GENTLY': true })
],
module: {
rules: [
{

File diff suppressed because it is too large Load Diff