Skip to content

Commit

Permalink
fix: 修复评论区无法显示定位问题
Browse files Browse the repository at this point in the history
  • Loading branch information
eeelester committed Dec 26, 2024
1 parent 580f69d commit 364b754
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 96 deletions.
50 changes: 25 additions & 25 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ on:
branches:
- main
paths:
- "**.js"
- "**.ts"
- "**.tsx"
- "**.less"
- ".github/workflows/**"
- '**.js'
- '**.ts'
- '**.tsx'
- '**.less'
- '.github/workflows/**'
pull_request:
branches:
- main
paths:
- "**.js"
- "**.ts"
- "**.tsx"
- "**.less"
- ".github/workflows/**"
- '**.js'
- '**.ts'
- '**.tsx'
- '**.less'
- '.github/workflows/**'

jobs:
CI:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: latest
run_install: true
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: latest
run_install: true

- name: Lint
run: pnpm lint
- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm

- name: Lint
run: pnpm lint
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### 这是一款chrome插件,功能如下:

- 在bilibili网页上看全屏/网页全屏时直播时能够控制是否显示醒目留言(SC)
- 评论区显示IP属地(来源于[Web-Show-IP-Location](https://github.com/maxchang3/Bilibili-Web-Show-IP-Location)

Expand Down Expand Up @@ -31,5 +32,5 @@ pnpm build
---

### 感谢
#### 评论显示IP属地使用的是: [Web-Show-IP-Location](https://github.com/maxchang3/Bilibili-Web-Show-IP-Location)

#### 评论显示IP属地使用的是: [Web-Show-IP-Location](https://github.com/maxchang3/Bilibili-Web-Show-IP-Location)
1 change: 0 additions & 1 deletion entrypoints/fullScreen.content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ export default defineContentScript({

// 监听网页全屏模式
ObservePageFullScreen()

},
})
16 changes: 7 additions & 9 deletions entrypoints/ipLocation.content/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@


export default defineContentScript({

Check warning on line 1 in entrypoints/ipLocation.content/index.tsx

View workflow job for this annotation

GitHub Actions / CI

Fast refresh can't handle anonymous components. Add a name to your export
matches: ['https://bilibili.com/*', 'https://*.bilibili.com/*'],
main() {
const script = document.createElement("script");
// @ts-ignore: 实际有
script.src = browser.runtime.getURL("bilibili-web-show-ip-location.user.js"); // 根据实际路径调整
const script = document.createElement('script')
// @ts-expect-error: 实际有
script.src = browser.runtime.getURL('bilibili-web-show-ip-location.user.js') // 根据实际路径调整

script.onload = function () {
console.log("Local script loaded successfully!");
script.remove();
};
console.log('Local script loaded successfully!')
script.remove()
}

document.head.appendChild(script);
document.head.appendChild(script)
},
})
5 changes: 3 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ module.exports = antfu(
ignores: [
'*.config.js',
'*.config.ts',
'test/**'
'test/**',
'public/**'
]
},
{
files: ['**/*.tsx', '**/*.ts'],
rules: {
"react-hooks/exhaustive-deps": "warn",
"no-console":"off",
"no-console": "off",
"import/no-mutable-exports": "warn"
},
},
Expand Down
58 changes: 21 additions & 37 deletions public/bilibili-web-show-ip-location.user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name 哔哩哔哩网页版显示 IP 属地 B站 Bilibili IP 属地显示
// @namespace http://zhangmaimai.com
// @version 1.6.3
// @version 1.6.4
// @author MaxChang3
// @description 我不喜欢 IP 属地,但是你手机都显示了,为什么电脑不显示呢?显示网页版 B 站 IP 属地,支持大部分场景的评论区
// @license MIT
Expand All @@ -28,21 +28,16 @@

var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);
var _unsafeWindow = /* @__PURE__ */ (() => typeof window != "undefined" ? window : void 0)();
const isElementLoaded = async (selector, root = document) => {
const getElement = () => root.querySelector(selector);
return new Promise((resolve) => {
const element = getElement();
if (element)
return resolve(element);
if (element) return resolve(element);
const observer = new MutationObserver((_) => {
const element2 = getElement();
if (!element2)
return;
if (!element2) return;
resolve(element2);
observer.disconnect();
});
Expand All @@ -60,8 +55,7 @@
clearInterval(interval);
resolve(false);
}
if (!fn())
return;
if (!fn()) return;
clearInterval(interval);
resolve(true);
}, 100);
Expand All @@ -85,10 +79,8 @@
}
match(url) {
for (const { prefix, action, constrait } of this.routes) {
if (!url.startsWith(prefix))
continue;
if (constrait.endsWith && !url.endsWith(constrait.endsWith))
continue;
if (!url.startsWith(prefix)) continue;
if (constrait.endsWith && !url.endsWith(constrait.endsWith)) continue;
action();
break;
}
Expand All @@ -99,13 +91,11 @@
update() {
super.update();
const pubDateEl = this.shadowRoot.querySelector("#pubdate");
if (!pubDateEl)
return;
if (!pubDateEl) return;
let locationEl = this.shadowRoot.querySelector("#location");
const locationString = getLocationString(this.data);
if (!locationString) {
if (locationEl)
locationEl.remove();
if (locationEl) locationEl.remove();
return;
}
if (locationEl) {
Expand All @@ -124,8 +114,7 @@
const { define: originalDefine } = _unsafeWindow.customElements;
const applyHandler = (target, thisArg, args) => {
const [name, constructor, ...rest] = args;
if (typeof constructor !== "function" || name !== "bili-comment-action-buttons-renderer")
return Reflect.apply(target, thisArg, args);
if (typeof constructor !== "function" || name !== "bili-comment-action-buttons-renderer") return Reflect.apply(target, thisArg, args);
const PatchActionButtonsRender = createPatch(constructor);
return Reflect.apply(target, thisArg, [name, PatchActionButtonsRender, ...rest]);
};
Expand Down Expand Up @@ -175,8 +164,7 @@
};
const insertLocation = (replyItemEl) => {
const replyInfo = replyItemEl.className.startsWith("sub") ? replyItemEl.querySelector(".sub-reply-info") : replyItemEl.querySelector(".reply-info");
if (!replyInfo)
throw new Error("Can not detect reply info");
if (!replyInfo) throw new Error("Can not detect reply info");
const locationString = getLocationFromReply(replyItemEl);
if (locationString && replyInfo.children.length !== 0 && !replyInfo.children[0].innerHTML.includes("IP属地")) {
replyInfo.children[0].innerHTML += `  ${locationString}`;
Expand All @@ -187,17 +175,13 @@
const targetNode = await isElementLoaded(".reply-list", root);
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type !== "childList")
continue;
if (mutation.type !== "childList") continue;
mutation.addedNodes.forEach((node) => {
if (!isReplyItem(node))
return;
if (!isReplyItem(node)) return;
insertLocation(node);
if (node.className.startsWith("sub"))
return;
if (node.className.startsWith("sub")) return;
const subReplyListEl = node.querySelector(".sub-reply-list");
if (!subReplyListEl)
return;
if (!subReplyListEl) return;
const subReplyList = Array.from(subReplyListEl.children);
subReplyList.pop();
subReplyList.map(insertLocation);
Expand All @@ -211,8 +195,7 @@
let lastObserved;
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type !== "childList" || !(mutation.target instanceof HTMLElement) || !mutation.target.classList.contains("bili-comment-container") || mutation.target === lastObserved)
continue;
if (mutation.type !== "childList" || !(mutation.target instanceof HTMLElement) || !mutation.target.classList.contains("bili-comment-container") || mutation.target === lastObserved) continue;
observeAndInjectComments(mutation.target);
lastObserved = mutation.target;
}
Expand Down Expand Up @@ -248,8 +231,7 @@
return !!(readInfo && ((_a2 = readInfo.lastElementChild) == null ? void 0 : _a2.textContent) !== "--评论");
});
const publishText = articleDetail.querySelector(".publish-text");
if (!publishText || !((_b = (_a = articleDetail.__vue__) == null ? void 0 : _a.readViewInfo) == null ? void 0 : _b.location))
return;
if (!publishText || !((_b = (_a = articleDetail.__vue__) == null ? void 0 : _a.readViewInfo) == null ? void 0 : _b.location)) return;
publishText.innerHTML += `  IP属地:${articleDetail.__vue__.readViewInfo.location}`;
}
);
Expand Down Expand Up @@ -284,7 +266,7 @@
}, { endsWith: "/" });
router.serve("https://t.bilibili.com/", async () => {
const dynItem = await isElementLoaded(".bili-dyn-item");
const isNewDyn = dynItem.querySelector(".bili-dyn-item__footer");
const isNewDyn = !dynItem.querySelector(".bili-dyn-item__footer");
if (isNewDyn) {
hookLit();
} else {
Expand All @@ -293,6 +275,8 @@
});
router.serve("https://www.bilibili.com/blackroom/ban/", () => hookBBComment({ blackroom: true }));
router.serve("https://manga.bilibili.com/detail/", observeAndInjectComments);
router.match(location.href);
const { origin, pathname } = new URL(location.href);
const urlWithoutQueryOrHash = `${origin}${pathname}`;
router.match(urlWithoutQueryOrHash);

})();
1 change: 0 additions & 1 deletion utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { eventBus } from '@/utils/event'
import { WS_SC_EVENT } from '@/constant'


export interface DanmuDataProps {
data: {
user_info: {
Expand Down
20 changes: 0 additions & 20 deletions vite-plugin-replace.js

This file was deleted.

24 changes: 24 additions & 0 deletions vite-plugin-replace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import fs from 'node:fs'
import path from 'node:path'
import process from 'node:process'

interface PluginOptions {
regex: RegExp
replacement: string
}

export function ReplaceUnsafeWindowPlugin(options: PluginOptions) {
return {
name: 'vite-plugin-replace-unsafeWindow',
apply: 'build',
closeBundle() {
const publicDir = path.resolve(process.cwd(), 'public')
const targetFile = path.join(publicDir, 'bilibili-web-show-ip-location.user.js')
if (fs.existsSync(targetFile)) {
let content = fs.readFileSync(targetFile, 'utf-8')
content = content.replace(options.regex, options.replacement)
fs.writeFileSync(targetFile, content, 'utf-8')
}
},
}
}

0 comments on commit 364b754

Please sign in to comment.