Minimal working version (#1)
* cra boilerplate * Dummy chrome extension * eslint and working url popup * content script and dummy messaging * replace .obj diffs with dummy test * comment and in-order multiple type support * get pull api data from url * README title and desc * api/elements match with filename check * github token signin signout * manifest v3, service request for CORS * working kittycad api in service worker * First real background message * Clean up, better types * Fix settings * multiservice settings * Tweaks * WIP: download file * Working downloads and kittycad conversion * Inject react, add three dependencies * Working stl canvas * primer for github-like style * Loading before model * diff colors * colorMode auto * Popup clean up * clean up * Working loading * Logos * Add GitHub CI * Working test * yarn test in ci * Little tweak * Update README * component tests * Better test * Clean up * UserCard test * working caddiff test * Note * Rename App to Settings * storage test * Clean up * Clean up content script * further content cleanup * Fix test * Little tweaks to modelview * More tests and testing * Regex fix * LFS file download test * prettier config from kittycad/website * Little tweaks * comment * log level * Tweaks * README update * more prettier * comment * Irrelevant comment * No .vscode and readme update * Remove .vscode * Package.json update after vscode removal
This commit is contained in:
66
src/chrome/web.ts
Normal file
66
src/chrome/web.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import { supportedSrcFormats } from './diff'
|
||||
import { DiffEntry } from './types'
|
||||
|
||||
export type GithubUrlParams =
|
||||
| {
|
||||
owner: string
|
||||
repo: string
|
||||
pull: number
|
||||
}
|
||||
| undefined
|
||||
|
||||
export function getGithubUrlParams(url: string): GithubUrlParams {
|
||||
// TODO: support commit diff
|
||||
const pullRe =
|
||||
/https:\/\/github\.com\/([a-zA-Z0-9_.-]+)\/([a-zA-Z0-9_.-]+)\/pull\/(\d+)\/files/
|
||||
const result = pullRe.exec(url)
|
||||
if (!result) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const [, owner, repo, pull] = result
|
||||
return { owner, repo, pull: parseInt(pull) }
|
||||
}
|
||||
|
||||
export function getWebPullElements(document: Document): HTMLElement[] {
|
||||
const fileTypeSelectors = Array.from(supportedSrcFormats).map(
|
||||
t => `.file[data-file-type=".${t}"]`
|
||||
)
|
||||
const selector = fileTypeSelectors.join(', ')
|
||||
return [...document.querySelectorAll(selector)].map(n => n as HTMLElement)
|
||||
}
|
||||
|
||||
export function getElementFilename(element: HTMLElement) {
|
||||
const titleElement = element.querySelector(
|
||||
'.file-info a[title]'
|
||||
) as HTMLElement
|
||||
return titleElement.getAttribute('title')
|
||||
}
|
||||
|
||||
export function getInjectablePullElements(
|
||||
elements: HTMLElement[],
|
||||
files: DiffEntry[]
|
||||
) {
|
||||
if (elements.length !== files.length) {
|
||||
throw Error(
|
||||
`elements and files have different length. Got ${elements.length} and ${files.length}`
|
||||
)
|
||||
}
|
||||
|
||||
const injectableElements = []
|
||||
for (const [index, element] of elements.entries()) {
|
||||
const apiFile = files[index]
|
||||
const filename = getElementFilename(element)
|
||||
if (filename !== apiFile.filename) {
|
||||
throw Error(
|
||||
"Couldn't match API file with a diff element on the page. Aborting."
|
||||
)
|
||||
}
|
||||
const diffElement = element.querySelector(
|
||||
'.js-file-content'
|
||||
) as HTMLElement
|
||||
injectableElements.push({ element: diffElement, file: apiFile })
|
||||
}
|
||||
|
||||
return injectableElements
|
||||
}
|
Reference in New Issue
Block a user