Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ jobs:
uses: mnao305/chrome-extension-upload@v5.0.0
with:
file-path: github-loc.zip
extension-id: clfbbldiigihjkignlkngblahkpalmhh
extension-id: bdjkkjhffigoaeinkkpipfhjdccimecc
client-id: ${{ secrets.CLIENT_ID }}
client-secret: ${{ secrets.CLIENT_SECRET }}
refresh-token: ${{ secrets.REFRESH_TOKEN }}
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

## [Unreleased]

### Fixed

- LOC stat not showing after GitHub's repository page redesign, or showing up in wrong places like PR contents. The stat is now anchored to the repository's forks stat and cloned from it, so it matches GitHub's current styling in both narrow and wide layouts

## [1.0.8] - 2026-09-16

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# GitHub LOC

Simple yet elegant Google Chrome extension that allows you to view Lines Of Code of both public and private GitHub repositories with an option to exclude selected languages. Clicking on the stat will take you to the [ghloc.vercel.app](https://ghloc.vercel.app/DervexDev/github-loc) page with even more LOC details. Get the extension from the Chrome Web Store [here](https://chromewebstore.google.com/detail/github-loc/clfbbldiigihjkignlkngblahkpalmhh).
Simple yet elegant Google Chrome extension that allows you to view Lines Of Code of both public and private GitHub repositories with an option to exclude selected languages. Clicking on the stat will take you to the [ghloc.vercel.app](https://ghloc.vercel.app/DervexDev/github-loc) page with even more LOC details. Get the extension from the Chrome Web Store [here](https://chromewebstore.google.com/detail/git-loc/bdjkkjhffigoaeinkkpipfhjdccimecc).

## Extension In Action

Expand Down
Binary file added public/store/demo1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/store/demo2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/store/demo3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 0 additions & 46 deletions src/stat/Stat.tsx

This file was deleted.

215 changes: 127 additions & 88 deletions src/stat/index.ts
Original file line number Diff line number Diff line change
@@ -1,127 +1,166 @@
import "./stat.css"
import { locateRoot, injectStat, updateStat, updateLink, findAboutRoot, STAT_ID } from "./injector"
import { isPrivateRepository, removeStats, StatState, syncStats } from "./injector"
import { filterTree } from "../details/tree"
import { fetchLoc, LocData, loadLoc } from "./loader"
import { getTarget, getFilter, loadMatchFilter, now } from "./util"
import Stat from "./Stat"
import { getTarget, isRepoHomePath, loadMatchFilter, now } from "./util"

const FETCH_RATE_LIMIT = 10 * 60
const NAV_EVENTS = ["turbo:load", "turbo:render", "popstate"] as const
const NAV_EVENTS = ["turbo:load", "turbo:render", "popstate", "pageshow"] as const
const PLACEHOLDER = "…"

interface Current {
key: string
org: string
repo: string
branch: string
isPrivate: boolean
filter: string
locData: LocData | null
}

let current: Current | null = null
let runId = 0
let lastTarget = ""
let debounceTimer: ReturnType<typeof setTimeout> | undefined
let missingTimer: ReturnType<typeof setTimeout> | undefined
let syncTimer: ReturnType<typeof setTimeout> | undefined
let pendingForce = false

function publicHref(org: string, repo: string, branch: string, filter: string) {
const url = new URL(`https://ghloc.vercel.app/${org}/${repo}`)
if (branch) {
url.searchParams.set("branch", branch)
}
if (filter) {
url.searchParams.set("filter", filter)
}
return url.toString()
}

function targetKey(org: string, repo: string, branch: string) {
return `${org}/${repo}/${branch}`
function privateHref(org: string, repo: string, branch: string) {
const url = new URL(chrome.runtime.getURL("src/details/index.html"))
url.searchParams.set("org", org)
url.searchParams.set("repo", repo)
if (branch) {
url.searchParams.set("branch", branch)
}
return url.toString()
}

async function showLoc(stat: Element, locData: LocData, id: number) {
const filter = await loadMatchFilter()
function stateOf(target: Current): StatState {
const { org, repo, branch, isPrivate, filter, locData } = target

return {
href: isPrivate ? privateHref(org, repo, branch) : publicHref(org, repo, branch, filter),
isPrivate,
value: locData ? filterTree(locData, filter).loc.toLocaleString() : PLACEHOLDER,
}
}

async function load(target: Current, id: number) {
const { org, repo, branch } = target

target.filter = await loadMatchFilter()
if (id !== runId) {
return
}

updateStat(stat, filterTree(locData, filter).loc)
}
const cached = await loadLoc(org, repo, branch)
if (id !== runId) {
return
}

if (cached) {
target.locData = cached
render()

function main() {
locateRoot().then(([root, isPrivate]) => {
const [org, repo, branch] = getTarget()
if (!org || !repo) {
if (now() - cached.lastFetched < FETCH_RATE_LIMIT) {
return
}
}

const key = targetKey(org, repo, branch)
if (root.querySelector(`#${STAT_ID}`) && lastTarget === key) {
return
try {
const fresh = await fetchLoc(org, repo, branch)
if (id === runId) {
target.locData = fresh
render()
}
} catch (err) {
console.log("Failed to fetch LOC:", err)
}
}

function render() {
if (current) {
syncStats(current.org, current.repo, stateOf(current))
}
}

function sync(force = false) {
if (!isRepoHomePath(location.pathname)) {
current = null
runId++
removeStats()
return
}

const [org, repo, branch] = getTarget()
const key = `${org}/${repo}/${branch}`

lastTarget = key
if (force || !current || current.key !== key) {
const id = ++runId

const statJSX = Stat({
current = {
key,
org,
repo,
branch,
isPrivate,
})

const stat = injectStat(root, statJSX)

loadLoc(org, repo, branch).then((locData) => {
if (id !== runId) {
return
}

if (!isPrivate) {
getFilter().then((filter) => {
if (id === runId) {
updateLink(stat, filter)
}
})
}

if (locData) {
showLoc(stat, locData, id)

if (now() - locData.lastFetched < FETCH_RATE_LIMIT) {
return
}
}

fetchLoc(org, repo, branch)
.then((locData) => {
if (id !== runId) {
return
}

showLoc(stat, locData, id)
})
.catch((err) => {
console.log("Failed to fetch LOC:", err)
})
})
})
isPrivate: isPrivateRepository(),
filter: "",
locData: null,
}

load(current, id)
}

render()
}

function requestUpdate() {
clearTimeout(debounceTimer)
debounceTimer = setTimeout(main, 50)
// Throttled rather than debounced, so a page that keeps mutating
// (e.g. live timestamps) cannot postpone the sync indefinitely.
function requestSync(force = false) {
pendingForce ||= force
if (syncTimer !== undefined) {
return
}

syncTimer = setTimeout(() => {
const forced = pendingForce
syncTimer = undefined
pendingForce = false
sync(forced)
}, 50)
}

chrome.runtime.onMessage.addListener((message) => {
if (message === "github-loc:update") {
lastTarget = ""
requestUpdate()
requestSync()
}
})

for (const event of NAV_EVENTS) {
window.addEventListener(event, () => {
lastTarget = ""
requestUpdate()
})
}

window.addEventListener("pageshow", (event) => {
if (event.persisted) {
lastTarget = ""
requestUpdate()
chrome.storage.onChanged.addListener((changes, area) => {
if (area === "sync" && (changes.ignoredFiles || changes.accessToken)) {
requestSync(true)
}
})

new MutationObserver(() => {
clearTimeout(missingTimer)
missingTimer = setTimeout(() => {
if (document.getElementById(STAT_ID) || !findAboutRoot()) {
return
}
for (const event of NAV_EVENTS) {
window.addEventListener(event, () => requestSync())
}

lastTarget = ""
main()
}, 50)
}).observe(document.documentElement, { childList: true, subtree: true })
// GitHub renders (and re-renders) the sidebar client-side, so watch the DOM
// and re-sync whenever the stat goes missing, gets duplicated or the URL
// changes. Syncing is idempotent, so our own DOM changes settle immediately.
new MutationObserver(() => requestSync()).observe(document.documentElement, {
childList: true,
subtree: true,
})

main()
sync()
Loading
Loading