Skip to content

Commit

Permalink
fix: fix xpath bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zhbyak committed Mar 26, 2024
1 parent 0892859 commit 15d8567
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/components/twitterAdded/twitterPage/userEthIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { NumberDisplayer } from '../../NumberDisplayer';
import '../../../tailwind.css';

interface UserPagePriceProps {
twitterUsername: string; // 假设价格是一个数字
twitterUsername: string;
}

export const UserPagePrice: FC<UserPagePriceProps> = ({ twitterUsername }) => {
Expand Down
47 changes: 25 additions & 22 deletions src/content/addToTwitterHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ export const addTwitterComponent = () => {
};

export const addUserPagePriceComponent = () => {
const addByXPath = (xpath: string) => {
const specificElement = document.evaluate(
xpath,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue as Element;

if (specificElement) {
const priceContainer = document.createElement('span');
const root = createRoot(priceContainer);
root.render(
<div id={elementId}>
<UserPagePrice twitterUsername={username} />
</div>
);
specificElement.appendChild(priceContainer);
return true;
}
return false;
};
// 获取当前页面的 URL
const currentUrl = window.location.href.toLowerCase();
const username = currentUrl.split('/')[currentUrl.split('/').length - 1];
Expand All @@ -100,27 +122,8 @@ export const addUserPagePriceComponent = () => {
if (existingElement) {
return;
}

const priceContainer = document.createElement('span');

// Update XPath based on your structure
// 当用户个人主页有背景图或者没有背景图的时候,xpath不一致。
const xpath = `/html/body/div[1]/div/div/div[2]/main/div/div/div/div[1]/div/div[3]/div/div/div/div[1]/div[2]/div[1]/div/div[1]/div/div/span/span[2]`;

const specificElement = document.evaluate(
xpath,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue as Element;
console.log('specificElement', specificElement);
if (specificElement) {
const root = createRoot(priceContainer);
root.render(
<div id={elementId}>
<UserPagePrice twitterUsername={username} />
</div>
);
specificElement.appendChild(priceContainer);
}
const xpath2 = `/html/body/div[1]/div/div/div[2]/main/div/div/div/div[1]/div/div[3]/div/div/div/div[2]/div[2]/div[1]/div/div[1]/div/div/span/span[2]`;
if (!addByXPath(xpath)) addByXPath(xpath2);
};

0 comments on commit 15d8567

Please sign in to comment.