Skip to content

Instantly share code, notes, and snippets.

@woxtu
Last active January 26, 2023 21:07
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save woxtu/9e0b78b10c2e3d00189f3f98381bfa6c to your computer and use it in GitHub Desktop.
Save woxtu/9e0b78b10c2e3d00189f3f98381bfa6c to your computer and use it in GitHub Desktop.
Twitter インプレッション アクセスカウンター
const id = "kiriban-fuminige-kinshi";
const conversions = [
{ unit: "K", rate: 1000 },
{ unit: "M", rate: 1000000 },
{ unit: "B", rate: 1000000000 },
{ unit: "万", rate: 10000 },
];
function run(target) {
const elements = target.querySelectorAll("a[href$='analytics']");
for (const element of elements) {
if (element.querySelector(`span.${id}`) || /^\D/.test(element.textContent)) {
continue;
}
let count =
element.textContent
.replaceAll(/,/g, "")
.replace(/Views$/, "")
.trim() || "0";
const conversion = conversions.find(({ unit }) => count.endsWith(unit));
if (conversion) {
count = `${Math.trunc(Number(count.replace(/.$/, "")) * conversion.rate)}`;
}
count = [...count.padStart(8, "0")].map((c) => String.fromCodePoint(0x1fbf0 + c.codePointAt(0) - 0x30)).join("");
while (element.firstChild) {
element.removeChild(element.firstChild);
}
element.insertAdjacentHTML("beforeend", `<span class="${id}">${count}</span>`);
}
}
document.head.insertAdjacentHTML(
"beforeend",
`
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Symbols+2&display=swap" rel="stylesheet">
<style id="${id}">
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Symbols+2&display=swap');
span.${id} {
background-color: #000000;
border-color: #eeeeee;
border-style: outset;
color: #00ff00;
font-family: 'Noto Sans Symbols 2';
padding: 2px 2px 0px 2px;
}
</style>
`
);
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
run(mutation.target);
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
{
"manifest_version": 3,
"name": "アクセスカウンター",
"description": "キリ番踏み逃げ厳禁",
"version": "0.0.1",
"content_scripts": [
{
"js": ["main.js"],
"matches": ["https://twitter.com/*"]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment