(async () => {
const { DIGIT_MAP, LETTER_MAP, SYMBOL_MAP } = window.XIEYIN_CONSTS;
const text = window.getSelection().toString().trim();
const showAlert = (message, seconds=2) => {
const alertDiv = document.createElement(
'div'
);
alertDiv.style = `
position: fixed;
top: 20px;
right: 20px;
padding: 15px;
background:
#ff4444;
color: white;
border-radius: 5px;
z-index: 9999;
box-shadow: 0 2px 10px rgba(0,0,0,0.2);
font-family:
'Microsoft YaHei'
, sans-serif;
line-height: 1.5;
white-space: pre-wrap;
text-align: left;
max-width: 300px;
`;
alertDiv.textContent = message;
alertDiv.innerHTML = message.replace(/\n/g,
'<br>'
);
document.body.appendChild(alertDiv);
setTimeout(() => {
document.body.removeChild(alertDiv);
}, seconds * 1000);
};
function
processNonChinese(char) {
const lowerChar = char.toLowerCase();
if
(LETTER_MAP.hasOwnProperty(lowerChar))
return
LETTER_MAP[lowerChar];
if
(DIGIT_MAP.hasOwnProperty(char))
return
DIGIT_MAP[char];
if
(SYMBOL_MAP.hasOwnProperty(char))
return
SYMBOL_MAP[char];
return
''
;
};
const generateDetailText = (text, dangerWord) => {
const chunks = [];
const dangerLower = dangerWord.toLowerCase();
[...text].forEach(char => {
let
py =
''
;
if
(/[\u4e00-\u9fa5]/.test(char)) {
try
{
py = pinyin(char, { toneType:
'none'
, multiple:
true
})[0];
}
catch
{
py =
'?'
;
}
}
else
{
const converted = processNonChinese(char);
py = converted ? converted : char;
}
chunks.push({ char, py });
});
const initialChain = chunks.map(c => c.py[0]?.charAt(0) ||
''
).join(
''
).toLowerCase();
const matches = [];
let
pos = -1;
while
((pos = initialChain.indexOf(dangerLower, pos + 1)) !== -1) {
const segment = chunks
.slice(pos, pos + dangerWord.length)
.map(c => `${c.char}(${c.py})`)
.join(
''
);
matches.push(`${segment}`);
}
return
matches.join(
'\n'
) ||
'(位置无法定位)'
;
}
console.log(
'当前选中:'
, text);
const { pinyin } = pinyinPro;
let
initialChain = pinyin(text, {pattern:
'first'
, toneType:
'none'
, multiple:
true
}).replace(/\s+/g,
''
);
console.log(
'首字母链:'
, initialChain);
chrome.storage.sync.get([
'dangerousWords'
,
'alertSeconds'
,
'showDetail'
], (result) => {
const text = window.getSelection().toString().trim();
const dangerousPatterns = result.dangerousWords || [
'tm'
,
'sb'
];
const duration = result.alertSeconds || 3;
const showDetail = result.showDetail !==
false
;
let
foundWord =
''
;
let
detailText =
''
;
[...text].forEach(char => {
if
(!/[\u4e00-\u9fa5]/.test(char)) {
const converted = processNonChinese(char);
if
(converted) initialChain += converted[0];
}
});
const foundWords = dangerousPatterns.filter(word =>
initialChain.includes(word.toLowerCase())
);
if
(foundWords.length > 0) {
let
message =
'有雷!!'
;
if
(showDetail) {
message +=
'\n\n'
+ foundWords.map(word => {
return
`${generateDetailText(text, word)}`;
}).join(
'\n\n'
);
}
showAlert(message, duration);
}
else
{
showAlert(
'安全~'
, duration);
}
});
})();