Skip to content

Commit

Permalink
chore: Move load (#97)
Browse files Browse the repository at this point in the history
* Move load

* Revent to mv2

* Implement feedback

---------

Co-authored-by: Diana Fulga <[email protected]>
  • Loading branch information
dianafulga and Diana Fulga authored Feb 19, 2024
1 parent 7a42437 commit 57a3291
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 82 deletions.
25 changes: 17 additions & 8 deletions src/background/Background.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { runtime, tabs } from 'webextension-polyfill'
import { Runtime, runtime, tabs } from 'webextension-polyfill'

import { PaymentFlowService } from '@/background/grantFlow'

Expand All @@ -23,15 +23,24 @@ class Background {

subscribeToMessages() {
this.subscriptions = this.messageHandlers.map((handler: any) => {
const listener: any = async (message: EXTMessage) => {
const listener: any = (
message: EXTMessage,
sender: Runtime.MessageSender,
sendResponse: (res: any) => void,
) => {
if (handler.type === message.type) {
try {
await handler.callback(message.data, this)
} catch (error) {
console.log('[===== Error in MessageListener =====]', error)
return error
}
handler
.callback(message.data, this)
.then((res: any) => {
sendResponse(res)
})
.catch((error: any) => {
console.log('[===== Error in MessageListener =====]', error)
sendResponse(error)
})
}

return true
}

runtime.onMessage.addListener(listener)
Expand Down
3 changes: 3 additions & 0 deletions src/background/grantFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ export class PaymentFlowService {
this.continuationRequestToken = continuationRequest.continuationRequestToken

const currentTabId = await this.getCurrentActiveTabId()

await tabs.sendMessage(currentTabId ?? 0, { type: 'LOAD' })

await tabs.sendMessage(currentTabId ?? 0, { type: 'START_PAYMENTS' })
}

Expand Down
14 changes: 14 additions & 0 deletions src/content/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { runtime } from 'webextension-polyfill'

import { initMonetizationTagManager } from '@/utils/monetizationTagManager'

import { loadObserver } from './linksObserver'
import MessageListener from './messageListener'

runtime.onMessage.addListener(MessageListener)

// DEBUG PURPOSE
loadObserver()

// TBD - check logic
initMonetizationTagManager()
26 changes: 0 additions & 26 deletions src/content/index.tsx

This file was deleted.

15 changes: 7 additions & 8 deletions src/content/messageListener.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// import { Runtime } from 'webextension-polyfill'

import { PaymentSender } from '@/content/monetization'

const paymentSender = new PaymentSender()

export const onRequest = async (
msg: EXTMessage,
// sender: Runtime.SendMessageOptionsType,
): Promise<EXTResponse | undefined> => {
// console.log('~~~~~~~', msg)

export const onRequest = async (msg: EXTMessage): Promise<EXTResponse | undefined> => {
switch (msg.type) {
case 'LOAD': {
const monetizationTag = document.querySelector('link[rel="monetization"]')
monetizationTag?.dispatchEvent(new Event('load'))
break
}

case 'IS_MONETIZATION_READY': {
const monetizationTag = document.querySelector('link[rel="monetization"]')

Expand Down
13 changes: 13 additions & 0 deletions src/content/static/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { wm2Polyfill } from '@/content/static/polyfill'

function inject(configure: (_script: HTMLScriptElement) => void) {
const script = document.createElement('script')
configure(script)
document.documentElement.appendChild(script)
document.documentElement.removeChild(script)
}

// eslint-disable-next-line @typescript-eslint/no-extra-semi
;(function injectCode(code: string) {
inject(script => (script.innerHTML = code))
})(wm2Polyfill)
2 changes: 0 additions & 2 deletions src/utils/polyfill.ts → src/content/static/polyfill.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// language=JavaScript
export const wm2Polyfill = `
var monetizationTag = document.querySelector('link[rel="monetization"]');
monetizationTag.dispatchEvent(new Event('load'));
const dbg = () => {
}
dbg('setonmonetization property start')
Expand Down
70 changes: 34 additions & 36 deletions src/manifest/chrome-v3.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
{
"name": "__MSG_appName__",
"version": "1.0.1",
"manifest_version": 3,
"description": "__MSG_appDescription__",
"icons": {
"34": "assets/icons/icon-34.png",
"128": "assets/icons/icon-128.png"
"name": "__MSG_appName__",
"version": "1.0.1",
"manifest_version": 3,
"description": "__MSG_appDescription__",
"icons": {
"34": "assets/icons/icon-34.png",
"128": "assets/icons/icon-128.png"
},
"default_locale": "en",
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content/content.js"]
},
"default_locale": "en",
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content/content.js"]
}
],
"background": {
"service_worker": "background/background.js"
},
"permissions": ["tabs", "storage"],
"host_permissions": ["http://*/*", "https://*/*"],
"options_ui": {
"page": "options/index.html"
},
"action": {
"default_icon": {
"16": "assets/icons/icon-16.png",
"48": "assets/icons/icon-48.png"
},
"default_title": "Web Monetization",
"default_popup": "popup/index.html"
},
"web_accessible_resources": [
{
"resources": ["assets/*", "content/*", "options/*", "popup/*", "background/*"],
"matches": ["<all_urls>"]
}
]
{
"run_at": "document_start",
"matches": ["http://*/*", "https://*/*"],
"js": ["contentStatic/contentStatic.js"]
}
],
"background": {
"service_worker": "background/background.js"
},
"permissions": ["tabs", "storage"],
"host_permissions": ["http://*/*", "https://*/*"],
"action": {
"default_title": "Web Monetization",
"default_popup": "popup/index.html"
},
"web_accessible_resources": [
{
"resources": ["assets/*", "content/*", "options/*", "popup/*", "background/*"],
"matches": ["<all_urls>"]
}
]
}
5 changes: 5 additions & 0 deletions src/manifest/chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
{
"matches": ["http://*/*", "https://*/*", "<all_urls>"],
"js": ["content/content.js"]
},
{
"run_at": "document_start",
"matches": ["http://*/*", "https://*/*"],
"js": ["contentStatic/contentStatic.js"]
}
],
"background": {
Expand Down
1 change: 1 addition & 0 deletions src/types/message.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare type EXTMessageType =
| 'STOP_PAYMENTS'
| 'PAYMENT_SUCCESS'
| 'PAUSE_PAYMENTS'
| 'LOAD'

declare type EXTMessage<T = any> = {
type: EXTMessageType
Expand Down
5 changes: 3 additions & 2 deletions webpack.config.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const EnvConfig: EnvironmentConfig = {
? Directories.DIST_DIR
: Directories.DEV_DIR,
...(process.env.NODE_ENV ? { NODE_ENV: process.env.NODE_ENV } : { NODE_ENV: 'development' }),
...(process.env.TARGET ? { TARGET: process.env.TARGET } : { TARGET: 'chrome' }),
...(process.env.TARGET ? { TARGET: process.env.TARGET } : { TARGET: 'chrome-v3' }),
}

/**
Expand Down Expand Up @@ -109,7 +109,8 @@ export const getOutput = (browserDir: string, outputDir = Directories.DEV_DIR) =
export const getEntry = (sourceDir = Directories.SRC_DIR) => {
return {
popup: [path.resolve(__dirname, `${sourceDir}/popup/index.tsx`)],
content: [path.resolve(__dirname, `${sourceDir}/content/index.tsx`)],
content: [path.resolve(__dirname, `${sourceDir}/content/index.ts`)],
contentStatic: [path.resolve(__dirname, `${sourceDir}/content/static/index.ts`)],
background: [path.resolve(__dirname, `${sourceDir}/background/index.ts`)],
}
}
Expand Down

0 comments on commit 57a3291

Please sign in to comment.