Skip to content
This repository has been archived by the owner on Jun 23, 2021. It is now read-only.

Commit

Permalink
feat: support for popups
Browse files Browse the repository at this point in the history
  • Loading branch information
sentialx committed Dec 27, 2019
1 parent e9a43a6 commit e7b9ed1
Show file tree
Hide file tree
Showing 12 changed files with 4,171 additions and 6,262 deletions.
6,244 changes: 0 additions & 6,244 deletions package-lock.json

This file was deleted.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@
],
"devDependencies": {
"@types/chrome": "0.0.91",
"@types/node": "12.12.17",
"@typescript-eslint/eslint-plugin": "^2.11.0",
"@typescript-eslint/parser": "^2.11.0",
"@types/node": "13.1.1",
"@typescript-eslint/eslint-plugin": "^2.13.0",
"@typescript-eslint/parser": "^2.13.0",
"cross-env": "^6.0.3",
"electron": "^7.1.5",
"eslint": "^6.7.2",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-prettier": "^3.1.1",
"electron": "^7.1.7",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.9.0",
"eslint-plugin-prettier": "^3.1.2",
"ncp": "^2.0.0",
"prettier": "1.19.1",
"rimraf": "^3.0.0",
"ts-loader": "^6.2.1",
"typescript": "3.7.3",
"webpack": "^4.41.2",
"typescript": "3.7.4",
"webpack": "^4.41.4",
"webpack-cli": "^3.3.10",
"webpack-merge": "^4.2.2"
},
Expand Down
1 change: 1 addition & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const getAPI = (extension: IpcExtension, sessionId: number) => {

extension: {
isIncognitoContext: false,
getURL: getRuntime(extension, sessionId).getURL,
},
};

Expand Down
3 changes: 3 additions & 0 deletions src/api/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ export const getStorage = (extensionId: string, sessionId: number) => ({
local: getStorageArea(extensionId, 'local', sessionId),
managed: getStorageArea(extensionId, 'managed', sessionId),
sync: getStorageArea(extensionId, 'sync', sessionId),
onChanged: {
addListener: () => {},
},
});
6 changes: 3 additions & 3 deletions src/background-preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IpcExtension } from '../models/ipc-extension';
declare const window: any;
declare const global: any;

global.isBackground = true;
global.isTab = false;

// https://github.com/electron/electron/issues/11290#issuecomment-362301961
Object.defineProperty(window.navigator, 'userAgent', {
Expand All @@ -30,8 +30,8 @@ const extension: IpcExtension = ipcRenderer.sendSync(
process.once('loaded', () => {
const api = getAPI(extension, sessionId);

window.chrome = api;
window.browser = api;
window.chrome = window.browser = api;
window.webext = api;

process.once('loaded', () => {
delete global.require;
Expand Down
2 changes: 1 addition & 1 deletion src/content-preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const arg = process.argv.find(x => x.startsWith('--blacklist='));

let blackList: string[] = [];

global.isBackground = false;
global.isTab = true;

if (arg) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/content-preload/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const injectChromeApi = async (
},
]);

w.chrome = w.browser = context;
w.chrome = w.browser = w.webext = context;
};

const runContentScript = async (
Expand Down
14 changes: 12 additions & 2 deletions src/main/services/messaging.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { webContents, ipcMain, BrowserWindow } from 'electron';
import { webContents, ipcMain, BrowserWindow, session } from 'electron';
import { getIpcExtension, sendToBackgroundPages } from '../../utils/extensions';
import { ExtensibleSession, storages } from '..';
import {
Expand Down Expand Up @@ -175,12 +175,22 @@ export const runMessagingService = (ses: ExtensibleSession) => {
}
});
} else {
const contents = getAllWebContentsInSession(ses.session);
let contents = getAllWebContentsInSession(ses.session);
for (const content of contents) {
if (content.id !== e.sender.id) {
content.send(`api-port-postMessage-${portId}`, msg, tab);
}
}

contents = getAllWebContentsInSession(
session.fromPartition(`persist:electron-extension-${ses.id}`),
);

for (const content of contents) {
if (content.id !== e.sender.id && webContentsValid(content)) {
content.send(`api-port-postMessage-${portId}`, msg, tab);
}
}
}
},
);
Expand Down
4 changes: 2 additions & 2 deletions src/models/port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ export class Port {
}

public postMessage(msg: any) {
const { isBackground } = global as any;
const { isTab } = global as any;

ipcRenderer.send(`api-port-postMessage-${this.sessionId}`, {
portId: this.portId,
msg,
tab: isBackground ? null : getSenderTab(),
tab: isTab ? null : getSenderTab(),
});
}
}
65 changes: 65 additions & 0 deletions src/popup-preload/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ipcRenderer, webFrame } from 'electron';
import { parse } from 'url';
import { getAPI } from '../api';
import { IpcExtension } from '../models/ipc-extension';

declare const global: any;

global.isTab = false;

let sessionId = -1;

const arg = process.argv.find(x => x.startsWith('--session-id='));

if (arg) {
try {
sessionId = parseInt(arg.split('--session-id=')[1]);
} catch (e) {
console.log(e);
}
}

// https://github.com/electron/electron/issues/11290#issuecomment-362301961
Object.defineProperty(window.navigator, 'userAgent', {
value: window.navigator.userAgent.replace(/Electron\/\S*\s/, ''),
configurable: false,
writable: false,
});

ipcRenderer.setMaxListeners(0);

const extensionId = parse(window.location.href).hostname;

const extension: IpcExtension = ipcRenderer.sendSync(
`get-extension-${sessionId}`,
extensionId,
);

const updateBounds = () => {
ipcRenderer.sendToHost(
'webview-size',
document.body.clientWidth,
document.body.clientHeight,
);
};

window.addEventListener('DOMContentLoaded', () => {
setTimeout(() => {
updateBounds();
});

document.body.addEventListener('resize', () => {
updateBounds();
});
});

window.addEventListener('blur', () => {
ipcRenderer.sendToHost('webview-blur');
});

process.once('loaded', async () => {
const api = getAPI(extension, sessionId);

const w: any = await webFrame.executeJavaScript('window');
w.chrome = w.browser = w.webext = api;
});
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const preloadsConfig = getConfig({
entry: {
'content-preload': './src/content-preload',
'background-preload': './src/background-preload',
'popup-preload': './src/popup-preload',
},

plugins: [],
Expand Down
Loading

0 comments on commit e7b9ed1

Please sign in to comment.