Skip to content

Improve Chrome extension scan packaging and filtering#79

Closed
doggy8088 wants to merge 3 commits into
sysprog21:mainfrom
doggy8088:main
Closed

Improve Chrome extension scan packaging and filtering#79
doggy8088 wants to merge 3 commits into
sysprog21:mainfrom
doggy8088:main

Conversation

@doggy8088
Copy link
Copy Markdown
Contributor

@doggy8088 doggy8088 commented May 5, 2026

變更摘要

本 PR 延續 Chrome extension 掃描器的後續整理,重點放在擴充套件打包流程、WASM 載入穩定性,以及只影響 extension UI 的掃描結果過濾。這些變更不調整 Rust CLI / MCP server 的核心掃描規則;新增的忽略與顯示邏輯都限制在 extension/ 的瀏覽器端結果處理。

詳細變更紀錄

Chrome extension WASM 打包流程

  • 調整 extension/build-wasm.sh,讓 WASM 建置輸出更符合 Chrome extension 載入需求。
  • 針對 Chrome package layout 修正 CI 流程,確保 extension package 包含必要檔案並排除不應放入發佈包的內容。
  • 改善 extension/src/scanner.js 的 WASM 載入流程,使用 extension 內的 dist/zhtw_mcp_wasm_bg.wasm 路徑,降低載入 glue code / wasm binary 時的路徑錯誤風險。

Extension 專屬掃描結果過濾

  • 新增 filterExtensionIgnoredIssues(),在 WASM 掃描結果回傳到 popup / highlight 前進行 extension-only 後處理。
  • 忽略網頁與文件常見 markup pattern:
    • ...
    • :::
  • 若 scanner 回報的是上述 pattern 內的局部 issue,例如 ::: 內單一 :,也會一併忽略,避免 popup 與頁面 highlight 出現雜訊。
  • 過濾後會重新計算:
    • issues
    • issue_count
    • badge_count
    • severity_counts

Popup INFO 顯示修正

  • 修正 popup 會顯示空白 INFO 卡片的問題。
  • 根因是部分 INFO issue 代表「刪除 / 插入 / 結構提示」類型,WASM 可能回傳 suggestions: [""] 或空白字串;原本 popup 只檢查 suggestions.length,導致畫面顯示 ai_style →punctuation → 這類沒有實質訊息的項目。
  • 現在 extension 端會:
    • 清除空白 suggestion。
    • 隱藏 severity === "info" 且沒有任何可見建議的 issue。
    • 保留 warning / error,即使 suggestion 為空也不自動隱藏,避免漏掉較高嚴重性的訊息。

文件與測試

  • 更新 extension/README.md,說明 extension 有 browser-only result filtering,且不影響 Rust CLI / MCP server / WASM scanner 規則。
  • 擴充 extension/test/shared.test.js,覆蓋:
    • ... / ::: pattern 忽略。
    • pattern 範圍內局部 issue 忽略。
    • 無可見建議的 INFO issue 隱藏。
    • 過濾後 badge / severity 統計重算。

驗證

npm test --prefix extension

測試結果:6 tests passed。


Summary by cubic

Adds a Chrome MV3 extension with stable WASM packaging/loading and browser-only result filtering to reduce UI noise. CLI/MCP scan rules are unchanged.

  • New Features

    • Add MV3 extension (background, content, popup, styles, icons) with highlights, badge counts, profiles, and a relaxed mode.
    • Package the scanner to browser WASM via extension/build-wasm.sh using wasm-pack; loader resolves dist/zhtw_mcp_wasm_bg.wasm via chrome.runtime.getURL.
    • Add extension-only filterExtensionIgnoredIssues() to ignore ..., ::: and issues inside them; recompute issues, issue_count, badge_count, and severity_counts.
    • Expose wasm-bindgen entrypoint scan_text behind the browser-wasm feature; include src/wasm.rs.
    • Add Extension CI to build WASM, run npm test --prefix extension, assemble unpacked/zip artifacts, validate dist assets, and upload them.
  • Bug Fixes

    • Hide empty INFO cards by trimming blank suggestions; only hide info, keep warning/error.
    • Improve WASM build/load reliability: auto-provision wasm32-unknown-unknown in build-wasm.sh, add a cleanup trap, and use explicit WASM init imports in the loader.
    • Fix Chrome package zip root and validate manifest.json and dist assets in CI.

Written for commit 834a1be. Summary will update on new commits.

Copilot AI review requested due to automatic review settings May 5, 2026 03:26
@doggy8088 doggy8088 closed this May 5, 2026
@doggy8088 doggy8088 reopened this May 5, 2026
@doggy8088 doggy8088 marked this pull request as draft May 5, 2026 03:29
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a Chrome Manifest V3 extension packaging/CI pipeline and a browser-targeted WASM entrypoint, plus extension-only post-filtering of scan results so common markup punctuation noise doesn’t surface in the popup or page highlights.

Changes:

  • Introduce a browser-wasm build (with feature-gated Rust modules) and a WASM-exported scan_text() API for the extension.
  • Add extension-side result filtering/hiding logic (ignored punctuation patterns + hide “info” issues with no visible suggestions) with Node tests.
  • Add extension MV3 implementation (background/content/popup), styles, assets, and a CI workflow that builds and assembles a distributable zip.

Reviewed changes

Copilot reviewed 22 out of 31 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/wasm.rs Adds wasm-bindgen entrypoint and JSON serialization for browser scanning.
src/rules/mod.rs Feature-gates native-only rules modules.
src/rules/loader.rs Switches ruleset hash computation to use blake3 directly (removes native-only audit dependency).
src/main.rs Refactors cfg(feature="translate") conditionals for cache/text retention logic.
src/lib.rs Feature-gates native modules and exposes wasm module under browser-wasm.
src/engine/scan/mod.rs Suppresses dead_code warnings for context helpers under browser-wasm.
extension/test/shared.test.js Adds tests for extension-only filtering, hiding, and recomputed counters.
extension/styles/popup.css Adds popup UI styling.
extension/styles/content.css Adds page highlight styling for injected marks.
extension/src/shared.js Adds shared helper functions (UTF-8 offset mapping, filtering/hiding, counters).
extension/src/scanner.js Adds WASM loader + scan wrapper that applies extension-only filtering.
extension/src/popup.js Adds popup UI logic to trigger scans and render results.
extension/src/content.js Adds content script to collect visible text and apply highlights.
extension/src/background.js Adds MV3 service worker orchestration, storage, badge updates, and highlighting.
extension/scripts/generate-icons.sh Adds a script to generate icon PNGs from the SVG source.
extension/README.md Documents building/loading/testing the extension and clarifies browser-only filtering.
extension/popup.html Adds popup markup for controls and results list.
extension/package.json Adds Node test script for extension helper tests.
extension/manifest.json Adds MV3 manifest configuration, permissions, and CSP for WASM.
extension/icons/source.svg Adds the source icon artwork.
extension/build-wasm.sh Adds WASM build script for generating extension/dist/ outputs.
Cargo.toml Introduces native/browser-wasm features, optional deps, and bin gating.
Cargo.lock Updates lockfile for new optional WASM-related dependencies.
.gitignore Ignores generated extension/dist/* outputs and a couple of repo artifacts.
.github/workflows/extension-ci.yml Adds CI job to build WASM, test helpers, and assemble/upload extension artifacts.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread extension/src/shared.js
Comment on lines +50 to +67
function filterExtensionIgnoredIssues(scanResult, text = "") {
const issues = Array.isArray(scanResult?.issues) ? scanResult.issues : [];
const ignoredRanges = ignoredPatternRanges(text);
const filteredIssues = issues
.map(normalizeDisplayIssue)
.filter(
(issue) =>
!shouldIgnoreExtensionIssue(issue, ignoredRanges) &&
!shouldHideExtensionIssue(issue),
);

return {
...scanResult,
issues: filteredIssues,
issue_count: filteredIssues.length,
badge_count: countBadgeIssues(filteredIssues),
severity_counts: countSeverityIssues(filteredIssues),
};
Comment thread extension/src/shared.js
Comment on lines +107 to +123
function ignoredPatternRanges(text) {
if (!text) {
return [];
}

const ranges = [];
for (const pattern of extensionIgnoredPatterns) {
let searchStart = 0;
while (searchStart < text.length) {
const index = text.indexOf(pattern, searchStart);
if (index < 0) {
break;
}
const byteStart = utf8ByteLength(text.slice(0, index));
ranges.push({
byteStart,
byteEnd: byteStart + utf8ByteLength(pattern),
Comment thread extension/src/content.js
Comment on lines +198 to +208
function issueToRanges(issue) {
if (!issue.length) {
return [];
}
const endByte = issue.offset + issue.length;
const startIndex = lastTextMap.findIndex(
(item) => issue.offset >= item.byteStart && issue.offset < item.byteEnd,
);
const endIndex = lastTextMap.findIndex(
(item) => endByte > item.byteStart && endByte <= item.byteEnd,
);
doggy8088 added 3 commits May 5, 2026 11:34
- Add automatic `wasm32-unknown-unknown` target provisioning in `build-wasm.sh` using temporary symlink directory to ensure environment compatibility.
- Register cleanup trap for temporary build artifacts.
- Update `scanner.js` to use explicit WASM initialization imports for improved module loading consistency.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants