Skip to content

Commit

Permalink
feat:去除走势图界面的广告弹窗
Browse files Browse the repository at this point in the history
  • Loading branch information
刘朋 committed Oct 23, 2024
1 parent 919b6cd commit d83e62f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@
"cheerio": "1.0.0-rc.6",
"compare-versions": "^3.6.0",
"entities": "2.2.0",
"http-proxy-middleware": "^3.0.3",
"iconv-lite": "^0.6.2",
"lodash": "4.17.21",
"moment": "^2.29.4",
Expand Down
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { StatusBar } from './statusbar/statusBar';
import { cacheStocksRemindData } from './webview/leekCenterView';
import { cacheFundAmountData, updateAmount } from './webview/setAmount';
import { cacheStockPriceData, updateStockPrice } from './webview/setStockPrice';
import { startProxyServer } from './webview/proxyService/proxyService';

let loopTimer: NodeJS.Timer | null = null;
let binanceLoopTimer: NodeJS.Timer | null = null;
Expand Down Expand Up @@ -216,6 +217,9 @@ export function activate(context: ExtensionContext) {
// register command
registerCommandPaletteEvent(context, statusBar);

// start local proxy server
startProxyServer();

// Telemetry Event
telemetry.sendEvent('activate');
}
Expand Down
20 changes: 20 additions & 0 deletions src/webview/proxyService/proxyConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 定义端口号变量,初始值为 undefined
let PORT: number | undefined = undefined;

/**
* 设置端口号
*/
export function setPort(port: number) {
PORT = port;
}

/**
* 获取代理Host
*/
export function getEastmoneyHost() {
if(PORT === undefined) {
return `https://quote.eastmoney.com`;
} else {
return `http://localhost:${PORT}`;
}
}
64 changes: 64 additions & 0 deletions src/webview/proxyService/proxyService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

import { setPort } from './proxyConfig';
const http = require('http');
const { createProxyMiddleware } = require('http-proxy-middleware');

// 启动 quote.eastmoney.com 的代理服务器
export async function startProxyServer() {
const PORT = await findAvailablePort(7100); // 从7100端口开始寻找

const server = http.createServer((req: any, res: any) => {
const proxy = createProxyMiddleware({
target: 'https://quote.eastmoney.com',
changeOrigin: true,
onProxyReq: (proxyReq: { setHeader: (arg0: string, arg1: string) => void; }, req: any) => {
// 设置 User-Agent 和 Cookie
proxyReq.setHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3');
// 设置 fullscreengg cookie解决页面频繁弹出广告
proxyReq.setHeader('Cookie', 'fullscreengg=1');
},
onError: (err: any, req: any, res: { writeHead: (arg0: number, arg1: { 'Content-Type': string; }) => void; end: (arg0: string) => void; }) => {
console.error('Proxy error:', err);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Something went wrong while proxying the request.');
},
});
proxy(req, res);
});

server.listen(PORT, () => {
const address = server.address();
const port = typeof address === 'string' ? 0 : address?.port;

if (port) {
setPort(port); // 设置端口号
console.log(`Proxy server running at http://localhost:${port}`);
}
console.log(`Proxy server running at http://localhost:${PORT}`);
});
}
/**
* 检测端口是否可用
*/
function isPortAvailable(port: number): Promise<boolean> {
return new Promise((resolve) => {
const server = http.createServer();
server.listen(port, () => {
server.close(() => resolve(true));
}).on('error', () => resolve(false));
});
}

/**
* 找到未占用的端口
*/
async function findAvailablePort(startPort = 7100): Promise<number> {
let port = startPort;
while (!(await isPortAvailable(port))) {
port++;
}
return port;
}


export default startProxyServer;
5 changes: 3 additions & 2 deletions src/webview/stockTrend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ViewColumn } from 'vscode';
import ReusedWebviewPanel from './ReusedWebviewPanel';
import stockTrendPic from './stockTrendPic';
import globalState from '../globalState';
import { getEastmoneyHost } from './proxyService/proxyConfig';

function stockTrend(code: string, name: string, stockCode: string) {
if (['0dji', '0ixic', '0inx'].includes(code)) {
Expand All @@ -24,7 +25,7 @@ function stockTrend(code: string, name: string, stockCode: string) {
}

let mcid = market + '.' + code.substr(1);
let url = `https://quote.eastmoney.com/basic/full.html?mcid=${mcid}`;
let url = `${getEastmoneyHost()}/basic/full.html?mcid=${mcid}`;

if (!!globalState.kLineChartSwitch) {
if (
Expand All @@ -33,7 +34,7 @@ function stockTrend(code: string, name: string, stockCode: string) {
stockCode.indexOf('sz399') !== 0
) {
// 沪深股票详情地址可查看盘前盘后指数、买五卖五、筹码分布
url = `https://quote.eastmoney.com/basic/h5chart-iframe.html?code=${code.substr(1)}&market=${market}`;
url = `${getEastmoneyHost()}/basic/h5chart-iframe.html?code=${code.substr(1)}&market=${market}`;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/webview/stockTrendPic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ViewColumn } from 'vscode';
import ReusedWebviewPanel from './ReusedWebviewPanel';

import { getEastmoneyHost } from './proxyService/proxyConfig';
function stockTrendPic(code: string, name: string, stockCode: string) {
const panel = ReusedWebviewPanel.create(
'stockTrendPicWebview',
Expand Down Expand Up @@ -59,7 +59,7 @@ function stockTrendPic(code: string, name: string, stockCode: string) {
panel.webview.html = panel.webview.html = `<html><body style="background:#eee;color:#333">
<br/>
<p style="text-align: center; font-size:18px; width: 400px;margin: 0 auto;">「${name}」趋势图、K线图</p>
<a style="position: absolute;right: 22px;top: 22px;font-size: 12px;" href="https://quote.eastmoney.com/${imageName}.html#fullScreenChart">网页全屏查看>></a>
<a style="position: absolute;right: 22px;top: 22px;font-size: 12px;" href="${getEastmoneyHost()}/${imageName}.html#fullScreenChart">网页全屏查看>></a>
<hr />
<h3 style="display:inline-block">实时走势图</h3><span style="margin-left:10px;color:#888;font-size:12px;" id="refreshtime">&nbsp;</span>
<br/><br/>
Expand Down

0 comments on commit d83e62f

Please sign in to comment.