Greasy Fork is available in English.

理杏仁助手

雪球、理性仁等网站之间跳转

Od 10.01.2024.. Pogledajte najnovija verzija.

// ==UserScript==
// @name         理杏仁助手
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  雪球、理性仁等网站之间跳转
// @author       chen
// @include      /^https:\/\/(xueqiu|www\.lixinger)+\.com.*/
// @grant        none
// @license MIT
// ==/UserScript==

/*eslint-disable*/
;
(function() {
	'use strict'

const hkStockTemplateUrl = 'https://www.lixinger.com/analytics/company/detail/hk/{stockCode}/{tickerId}/fundamental/valuation/primary'
const szStockTemplateUrl = 'https://www.lixinger.com/analytics/company/detail/sz/{stockCode}/{tickerId}/fundamental/valuation/primary'
const shStockTemplateUrl = 'https://www.lixinger.com/analytics/company/detail/sh/{stockCode}/{tickerId}/fundamental/valuation/primary'
const bjStockTemplateUrl = 'https://www.lixinger.com/analytics/company/detail/bj/{stockCode}/{tickerId}/fundamental/valuation/primary'

const szFundTemplateUrl = 'https://www.lixinger.com/analytics/fund/detail/sz/{stockCode}/{tickerId}/valuation/fitting'
const shFundTemplateUrl = 'https://www.lixinger.com/analytics/fund/detail/sh/{stockCode}/{tickerId}/valuation/fitting'

const szIndexTemplateUrl = 'https://www.lixinger.com/analytics/index/detail/sz/{stockCode}/{tickerId}/fundamental/valuation/primary'
const shIndexTemplateUrl = 'https://www.lixinger.com/analytics/index/detail/sh/{stockCode}/{tickerId}/fundamental/valuation/primary'
const csiIndexTemplateUrl = 'https://www.lixinger.com/analytics/index/detail/csi/{stockCode}/{tickerId}/fundamental/valuation/primary'

const regList = [
	// 港股
	/^\d{5}$/,
	// 深市股票
	/^SZ(00|01|02|20|30)\d{4}$/,
	// 沪市股票
	/^SH(600|601|603|605|688|689|900)\d{3}$/,
	// 北交所
	/^BJ(8|4)\d{5}$/,
	// 深市基金
	/^SZ(15|16)\d{4}$/,
	// 沪市基金
	/^SH5\d{5}$/,
	// 深证指数
	/^SZ39\d{4}$/,
	// 沪市指数
	/^SH000\d{3}$/,
	// 中证指数
	/^CSI(\d|H)\d{5}$/
];

const urlList = [
	hkStockTemplateUrl,
	szStockTemplateUrl,
	shStockTemplateUrl,
	bjStockTemplateUrl,
	szFundTemplateUrl,
	shFundTemplateUrl,
	szIndexTemplateUrl,
	shIndexTemplateUrl,
	csiIndexTemplateUrl,
];

function normalizeCode(code) {
	return code && code.replace(/^(sz|sh|bj|csi)/i, '')
}

function getLixingrenStockPageUrl(code) {
	const stockCode = normalizeCode(code)
	// 美股不支持
	if (!stockCode) return ''

	for (let i = 0; i < regList.length; i++) {
		if (regList[i].test(code)) {
			return urlList[i].replace(/\{stockCode\}/g, stockCode).replace(/\{tickerId\}/g, stockCode.replace(/[a-z]+/gi, ''))
		}
	}

	return ''
}

function setHrefAttribute(el, code) {
	const url = getLixingrenStockPageUrl(code)
	if (!url) {
		console.log('不支持的证券代码:' + code)
		return
	}
	el.attr('href', url)
}

function insertAfterLxrIcon(el, code) {
	const url = getLixingrenStockPageUrl(code)
	if (!url) {
		console.log('不支持的证券代码:' + code)
		return
	};

	el.after(`<div style="float: left;margin: 0 20px">
                        <a class="lxr-icon" target="_blank">
                            <img style="width: 20px;height: 20px;vertical-align: middle" 
                                src="https://www.lixinger.com/static/img/logo50x50.png" 
                                style="vertical-align: middle;" />
                        </a>
                </div>`)
}

if (location.host === 'xueqiu.com') {
	/**
	 * 主页自选股跳转到理性仁
	 */
	const linkSelector = '#optional tr.sortable a.code'
	$(document.body).on('click', linkSelector, function(e) {
		var href = $(this).attr('href')
		if (href.indexOf('www.lixinger.com') > -1) {
			return
		}

		const code = href.replace('/S/', '')
		setHrefAttribute($(this), code)
	})

	/**
	 * 股票页跳转到理性仁
	 */
	const titleSelector = '#app .stock-name'

	insertAfterLxrIcon($(titleSelector).eq(0), location.pathname.replace('/S/', ''));

	$('.lxr-icon').on('mouseenter', function() {
		const code = location.pathname.replace('/S/', '')
		setHrefAttribute($(this), code)
	})

	return
}

/*
 * 理性仁相关功能
 */
if (location.host === 'www.lixinger.com') {
	setInterval(function() {
		const regex = /\/analytics\/(company|fund|index)\/detail\/(sh|sz|bj|csi|hk)\/(\w+)\/(\d+)\/.+/;
		const result = location.pathname.match(regex);
		const linkNode = document.querySelector('.xueqiu-link');
console.log({result,linkNode})

		if (!result) {
			linkNode?.remove();
			return;
		}
		if (linkNode) {
			if (linkNode.getAttribute("stock-code") === result[3]) {
				return;
			} else {
				linkNode.remove();
			}
		}
		const nameNode = document.querySelector('div.action-group.d-flex');
		if (!nameNode) {
			return
		}
		const market = (result[2] === 'hk' ? '' : result[2]).toUpperCase();
		const code = result[3].toUpperCase();
		const xueqiuUrl = 'https://xueqiu.com/S/' + market + code
		const a = document.createElement('a')
		a.innerHTML = `<img 
								src="https://assets.imedao.com/images/favicon.png" 
								style="width: 1.32rem;"
							/>`;
		a.setAttribute('href', xueqiuUrl)
		a.setAttribute('target', '_blank')
		a.style.cssText = 'margin-left: 0.5rem;'
		a.setAttribute("class", "xueqiu-link");
		a.setAttribute("stock-code", result[3]);
		nameNode.append(a)
	}, 3000)
}
})()