Skip to content

Commit

Permalink
feat: revamp plugin inject and make it actually work
Browse files Browse the repository at this point in the history
  • Loading branch information
IncognitoTGT committed Jul 16, 2024
1 parent cd31ab2 commit 47f4cc8
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meteorproxy",
"version": "1.0.2",
"version": "1.0.3",
"description": "The modern interception proxy you've been waiting for",
"type": "module",
"main": "./lib/index.cjs",
Expand Down
4 changes: 2 additions & 2 deletions src/bundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { rewriteHtml } from './rewrite/html'
import { rewriteJs } from './rewrite/js'
import { decodeURL, encodeURL } from './rewrite/url'

import { base64, plain, xor } from './codecs'
import { createOrigin } from './util/createOrigin'
import { formatUrl } from './util/formatUrl'
import { log } from './util/logger'
import { base64, xor, plain } from './codecs'

declare global {
interface Window {
Expand Down Expand Up @@ -43,4 +43,4 @@ const meteorBundle = {
}
}

self.$meteor = meteorBundle
self.$meteor = meteorBundle
5 changes: 2 additions & 3 deletions src/bundle/rewrite/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function rewriteHtml(content: string, origin: URL) {
for (const plugin of self.$meteor.config.plugins) {
if (plugin.filter.test(origin.href)) {
self.$meteor.util.log(`Running inject for ${plugin.name}`, 'teal')
const context = createContext(rendered)
const context = createContext(rendered, origin)
plugin.inject(context)

rendered = context.getModified()
Expand Down Expand Up @@ -88,8 +88,7 @@ function rewriteElement(element: Element, origin: URL) {
const scriptsToPush: (keyof Config['files'])[] = [
'client',
'bundle',
'config',
'codecs'
'config'
]

for (const script of scriptsToPush) {
Expand Down
27 changes: 4 additions & 23 deletions src/bundle/util/createContext.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
import type { Context } from '@/types'
export function createContext(html: string): Context {
export function createContext(html: string, origin: URL): Context {
let modified = html

function injectAtPosition(content: string, position: number) {
return modified.slice(0, position) + content + modified.slice(position)
}

return {
injectHead: (content: string) => {
const headCloseIndex = modified.indexOf('</head>')
if (headCloseIndex !== -1) {
modified = injectAtPosition(content, headCloseIndex)
}
},
injectTag: (tag: string) => {
const parser = new DOMParser().parseFromString(tag, 'text/xml')
for (const attr of ['src', 'href']) {
if (parser.children[0].hasAttribute(attr)) {
parser.children[0].setAttribute(
attr,
self.$meteor.rewrite.url.encode(
parser.children[0].getAttribute(attr),
self.$meteor.util.createOrigin()
)
)
}
}
parser.children[0].setAttribute('data-meteor-injected', 'true')
const tagString = parser.children[0].outerHTML
const headCloseIndex = modified.indexOf('</head>')
injectHTML: (tag, location = 'head', rewrite = true) => {
const tagString = rewrite ? self.$meteor.rewrite.html(tag, origin) : tag
const headCloseIndex = modified.indexOf(`</${location}>`)
if (headCloseIndex !== -1) {
modified = injectAtPosition(tagString, headCloseIndex)
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export const config: Config = {
name: 'exampleplugin',
filter: /https:\/\/example.com*/g,
inject(ctx) {
ctx.injectHead(`<meta name="meteor" content="meteor - epic proccy">`)
ctx.injectHTML(`
<meta name="meteor" content="meteor - epic proccy">
<script x-inject="true" src="data:application/javascript,console.log('pneis')"></script>
`)
},
async onRequest(request) {
request.headers.set('X-Proxy', 'Meteor')
Expand Down
7 changes: 5 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export interface Codec {
}

export interface Context {
injectHead: (content: string) => void
injectTag: (content: string) => void
injectHTML: (
content: string,
location?: 'body' | 'head',
rewrite?: boolean
) => void
getModified: () => string
}

0 comments on commit 47f4cc8

Please sign in to comment.