mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
feat: resolve platform async
This commit is contained in:
parent
85736300d8
commit
23cc6ba5b7
2 changed files with 36 additions and 24 deletions
|
|
@ -1,33 +1,35 @@
|
|||
import { withErrorLog } from 'analytics'
|
||||
import { Gitako } from 'components/Gitako'
|
||||
import { addMiddleware } from 'driver/connect'
|
||||
import { platform } from 'platforms'
|
||||
import { platform, resolvePlatformP } from 'platforms'
|
||||
import * as React from 'react'
|
||||
import * as ReactDOM from 'react-dom'
|
||||
import './content.scss'
|
||||
|
||||
if (platform.resolveMeta()) {
|
||||
addMiddleware(withErrorLog)
|
||||
resolvePlatformP.then(() => {
|
||||
if (platform.resolveMeta()) {
|
||||
addMiddleware(withErrorLog)
|
||||
|
||||
function init() {
|
||||
// injects a copy of stylesheets so that other extensions(e.g. dark reader) could read
|
||||
function injectStyles(url: string) {
|
||||
var linkElement = document.createElement('link')
|
||||
linkElement.rel = 'stylesheet'
|
||||
linkElement.setAttribute('href', url)
|
||||
document.head.appendChild(linkElement)
|
||||
function init() {
|
||||
// injects a copy of stylesheets so that other extensions(e.g. dark reader) could read
|
||||
function injectStyles(url: string) {
|
||||
var linkElement = document.createElement('link')
|
||||
linkElement.rel = 'stylesheet'
|
||||
linkElement.setAttribute('href', url)
|
||||
document.head.appendChild(linkElement)
|
||||
}
|
||||
|
||||
injectStyles(browser.extension.getURL('content.css'))
|
||||
|
||||
const SideBarElement = document.createElement('div')
|
||||
document.body.appendChild(SideBarElement)
|
||||
ReactDOM.render(<Gitako />, SideBarElement)
|
||||
}
|
||||
|
||||
injectStyles(browser.extension.getURL('content.css'))
|
||||
|
||||
const SideBarElement = document.createElement('div')
|
||||
document.body.appendChild(SideBarElement)
|
||||
ReactDOM.render(<Gitako />, SideBarElement)
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', init)
|
||||
} else {
|
||||
init()
|
||||
}
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', init)
|
||||
} else {
|
||||
init()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -6,16 +6,26 @@ const hosts: Record<'GitHub' | 'GitLab' | 'Gitee', string[]> = {
|
|||
GitLab: ['gitlab.com'],
|
||||
Gitee: ['gitee.com'],
|
||||
}
|
||||
|
||||
function isGitHub() {
|
||||
return hosts.GitHub.includes(window.location.host)
|
||||
}
|
||||
|
||||
function resolvePlatform(): Platform {
|
||||
async function resolvePlatform(): Promise<Platform> {
|
||||
if (isGitHub()) return GitHub
|
||||
return dummyPlatformForTypeSafety
|
||||
}
|
||||
|
||||
export const platform: Platform = resolvePlatform()
|
||||
let p: Platform = dummyPlatformForTypeSafety
|
||||
|
||||
export const resolvePlatformP = resolvePlatform()
|
||||
resolvePlatformP.then(platform => (p = platform))
|
||||
|
||||
export const platform: Platform = new Proxy<Platform>(dummyPlatformForTypeSafety, {
|
||||
get(target, key: keyof Platform) {
|
||||
return p[key]
|
||||
},
|
||||
})
|
||||
|
||||
export const errors = {
|
||||
SERVER_FAULT: 'Server Fault',
|
||||
|
|
|
|||
Loading…
Reference in a new issue