Skip to content

Commit

Permalink
Open window in content script
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyec committed May 24, 2024
1 parent 303fd1c commit 996c724
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 51 deletions.
53 changes: 6 additions & 47 deletions src/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,6 @@ function parseWindowFeatures(windowFeatures = '') {
return params;
}

async function invokeProxyFunction(name, args = []) {
console.log('invokeProxyFunction');

switch (name) {
case 'open': {
const [url = '', _target = '', windowFeatures = ''] = args;

if (
windowFeatures.includes('width') ||
windowFeatures.includes('height')
) {
const params = parseWindowFeatures(windowFeatures);
const left = params['left'] || params['screenX'];
const top = params['top'] || params['screenY'];
const width = params['width'] || params['innerWidth'];
const height = params['height'] || params['innerHeight'];
const type = params['toolbar'] === 'no' ? 'popup' : 'normal';

// TODO(anthony): Parse arguments and convert them to create options.
chrome.windows.create({
url,
type,
left,
top,
width,
height,
setSelfAsOpener: true
});
} else {
// TODO(anthony): Ensure tab is created after the last active tab.
chrome.tabs.create({ url });
}
break;
}
default:
console.error(`No proxy function found for: ${name}`);
}
}

async function queueAndReload(bookmarkId) {
const [activeTab] = await chrome.tabs.query({
active: true,
Expand Down Expand Up @@ -146,9 +107,12 @@ function getBookmarkletAsUserScript(id, title, url = '') {
`}`,
`function _${userScriptId}() {`,
` // ${title}`,
` const powerletOpen = (...args) => ${identifiers.invokeProxyFunction}("open", args);`,
` const window = { ...globalThis, open: powerletOpen };`,
` const open = powerletOpen;`,
// Chrome's popup blocker will stop windows from opening since this user
// script is executing in the "MAIN" world. To avoid this, `open` is
// overridden our own function for opening windows. This does not affect
// global `window.open` thanks to variable scoping.
` const open = (...args) => ${identifiers.invokeProxyFunction}("open", args);`,
` const window = { ...globalThis, open };`,
` ${bookmarkletCode}`,
'}'
);
Expand Down Expand Up @@ -229,7 +193,6 @@ async function main() {

chrome.runtime.onMessage.addListener(async (message) => {
if (!isObject(message) || !('type' in message)) return;
console.log('background', message);

if (message.type === identifiers.executeBookmarkletEvent) {
executeBookmarklet(message.id);
Expand All @@ -238,10 +201,6 @@ async function main() {
if (message.type === identifiers.queueAndReloadEvent) {
queueAndReload(message.id);
}

if (message.type === identifiers.invokeProxyFunction) {
invokeProxyFunction(message.name, message.args);
}
});
}

Expand Down
22 changes: 21 additions & 1 deletion src/content/isolated.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import * as identifiers from '../identifiers';
import { isObject } from '../utils/is_object';

function invokeProxyFunction(name, args = []) {
switch (name) {
case 'open': {
window.open(...args);
break;
}

default: {
console.error(`No proxy function found for: ${name}`);
}
}
}

// Bridge all runtime events from the extension to window events.
chrome.runtime.onMessage.addListener((message) => {
const event = new CustomEvent(identifiers.contentMessageEvent, {
Expand All @@ -16,5 +29,12 @@ window.addEventListener(identifiers.runtimeMessageEvent, (event) => {
return;
if (!('type' in event.detail)) return;

chrome.runtime.sendMessage(event.detail);
const message = event.detail;

if (message.type === identifiers.invokeProxyFunction) {
invokeProxyFunction(message.name, message.args);
return;
}

chrome.runtime.sendMessage(message);
});
2 changes: 0 additions & 2 deletions src/content/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ function handleMessage(message) {
}

window[identifiers.invokeProxyFunction] = (name, args = []) => {
// TODO(anthony): Find out why messages sent twice? Or are there 2 listeners?
console.log(identifiers.invokeProxyFunction);
sendMessage({ type: identifiers.invokeProxyFunction, name, args });
};

Expand Down
2 changes: 1 addition & 1 deletion src/popup/store/actions/bookmarklets.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function executeBookmarklet(id) {
id
});
} catch (err) {
console.log(err);
console.error(err);
}
};
}
Expand Down

0 comments on commit 996c724

Please sign in to comment.