Skip to content

Commit

Permalink
feat: support default user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Postcount CI committed Jun 13, 2023
1 parent 2a9b105 commit f0ae971
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 40 deletions.
1 change: 1 addition & 0 deletions env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ declare type GateFrameOption = {
position?: GateFrameOptionType
userAgent?: string
zoomFactor?: number
css?: string
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-open-gate",
"version": "1.8.3",
"version": "1.8.4",
"description": "Embedding any website to Obsidian, from now all, you have anything you need in one place.",
"main": "main.js",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/fns/createEmptyGateOption.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import getDefaultUserAgent from './getDefaultUserAgent'

export const createEmptyGateOption = (): GateFrameOption => {
return {
id: '',
Expand All @@ -8,6 +10,6 @@ export const createEmptyGateOption = (): GateFrameOption => {
profileKey: 'open-gate',
url: '',
zoomFactor: 1.0,
userAgent: ''
userAgent: getDefaultUserAgent()
}
}
10 changes: 3 additions & 7 deletions src/fns/createIframe.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
export const createIframe = (
params: Partial<GateFrameOption>
): HTMLIFrameElement => {
export const createIframe = (params: Partial<GateFrameOption>): HTMLIFrameElement => {
const iframe = document.createElement('iframe')

iframe.setAttribute('allowpopups', '')
iframe.setAttribute('credentialless', 'true')
iframe.setAttribute('src', params.url ?? 'about:blank')
iframe.setAttribute(
'sandbox',
'allow-scripts allow-same-origin allow-popups allow-forms'
)
iframe.setAttribute('sandbox', 'allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts allow-top-navigation-by-user-activation')
iframe.setAttribute('allow', 'encrypted-media; fullscreen; oversized-images; picture-in-picture; sync-xhr; geolocation')
iframe.addClass('open-gate-iframe')

return iframe
Expand Down
14 changes: 7 additions & 7 deletions src/fns/createWebviewTag.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import WebviewTag = Electron.WebviewTag

export const createWebviewTag = (
params: Partial<GateFrameOption>
): WebviewTag => {
const webviewTag = document.createElement(
'webview'
) as unknown as WebviewTag
export const createWebviewTag = (params: Partial<GateFrameOption>): WebviewTag => {
const webviewTag = document.createElement('webview') as unknown as WebviewTag
webviewTag.setAttribute('allowpopups', 'true')
webviewTag.setAttribute('partition', 'persist:' + params.profileKey)
webviewTag.setAttribute('src', params.url ?? 'about:blank')
Expand All @@ -15,7 +11,11 @@ export const createWebviewTag = (
webviewTag.setAttribute('useragent', params.userAgent)
}

webviewTag.addEventListener('did-attach', () => {
webviewTag.addEventListener('dom-ready', async () => {
if (params.css) {
await webviewTag.insertCSS(params.css)
}

if (params.zoomFactor) {
webviewTag.setZoomFactor(params.zoomFactor)
}
Expand Down
24 changes: 7 additions & 17 deletions src/fns/registerCodeBlockProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createIframe } from './createIframe'
import { createWebviewTag } from './createWebviewTag'
import WebviewTag = Electron.WebviewTag
import getDefaultUserAgent from './getDefaultUserAgent'
import { createEmptyGateOption } from './createEmptyGateOption'

export function registerCodeBlockProcessor(plugin: Plugin) {
plugin.registerMarkdownCodeBlockProcessor('gate', (sourceCode, el, ctx) => {
Expand All @@ -16,37 +17,26 @@ export function registerCodeBlockProcessor(plugin: Plugin) {
return
}

let src = ''
let height = 'fit-content'
let profileKey = 'open-gate'
let userAgent = getDefaultUserAgent()
let zoomFactor = 1

const options = createEmptyGateOption()
let height = '300px'
for (const line of lines) {
if (line.startsWith('http')) {
src = line.trim()
options.url = line.trim()
} else if (line.startsWith('height:')) {
height = line.replace('height:', '').trim()
// if height is a number, add px
if (!isNaN(Number(height))) {
height = height + 'px'
}
} else if (line.startsWith('profile:')) {
profileKey = line.replace('profile:', '').trim()
options.profileKey = line.replace('profile:', '').trim()
} else if (line.startsWith('useragent:')) {
userAgent = line.replace('useragent:', '').trim()
options.userAgent = line.replace('useragent:', '').trim()
} else if (line.startsWith('zoom:')) {
zoomFactor = parseFloat(line.replace('zoom:', '').trim())
options.zoomFactor = parseFloat(line.replace('zoom:', '').trim())
}
}

let frame: HTMLIFrameElement | WebviewTag
const options = {
profileKey: profileKey,
url: src,
userAgent: userAgent,
zoomFactor: zoomFactor
}

if (Platform.isMobileApp) {
frame = createIframe(options)
Expand Down
10 changes: 3 additions & 7 deletions src/fns/registerLinkProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@ export const registerLinkProcessor = (plugin: Plugin) => {
height = height + 'px'
}

const profileKey = altArr ? altArr[1]?.replace('profile:', '') : 'open-gate'
const useragent = altArr ? altArr[2]?.replace('useragent:', '') : getDefaultUserAgent()
const zoomFactor = altArr ? parseFloat(altArr[3]?.replace('zoom:', '') ?? '1') : 1

if (!src || isImageExt(src)) {
return
}

let frame: HTMLIFrameElement | WebviewTag
const options = {
profileKey: profileKey,
profileKey: altArr ? altArr[1]?.replace('profile:', '') : 'open-gate',
url: src,
userAgent: useragent,
zoomFactor: zoomFactor
userAgent: altArr ? altArr[2]?.replace('useragent:', '') : getDefaultUserAgent(),
zoomFactor: altArr ? parseFloat(altArr[3]?.replace('zoom:', '') ?? '1') : 1
}

if (Platform.isMobileApp) {
Expand Down

0 comments on commit f0ae971

Please sign in to comment.