Skip to content

Commit

Permalink
fix violentmonkey#1881: GM_log in Chrome 109+
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Sep 2, 2023
1 parent 9b2797e commit 386d596
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/injected/content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ addBackgroundHandlers({
});

addHandlers({
Log: data => safeApply(logging[data[0]], logging, data[1]),
TabFocus: REIFY,
UpdateValue: REIFY,
});
Expand Down
6 changes: 3 additions & 3 deletions src/injected/content/inject.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import bridge, { addHandlers } from './bridge';
import { elemByTag, makeElem, nextTask, onElement, sendCmd } from './util';
import { bindEvents, fireBridgeEvent, META_STR } from '../util';
import { bindEvents, CONSOLE_METHODS, fireBridgeEvent, META_STR } from '../util';
import { Run } from './cmd-run';

const bridgeIds = bridge[IDS];
Expand Down Expand Up @@ -308,7 +308,7 @@ async function injectPageList(runAt) {
}

function setupContentInvoker() {
const invokeContent = VMInitInjection(IS_FIREFOX)(bridge.onHandle);
const invokeContent = VMInitInjection(IS_FIREFOX)(bridge.onHandle, logging);
const postViaBridge = bridge.post;
bridge.post = (cmd, params, realm, node) => {
const fn = realm === CONTENT
Expand Down Expand Up @@ -348,7 +348,7 @@ function addVaultExports(vaultSrc) {
const exports = cloneInto(createNullObj(), document);
// In FF a detached iframe's `console` doesn't print anything, we'll export it from content
const exportedConsole = cloneInto(createNullObj(), document);
['log', 'info', 'warn', 'error', 'debug']::forEach(k => {
CONSOLE_METHODS::forEach(k => {
exportedConsole[k] = exportFunction(logging[k], document);
/* global exportFunction */
});
Expand Down
2 changes: 2 additions & 0 deletions src/injected/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export {
} from '@/common';
export * from '@/common/consts';

export const CONSOLE_METHODS = ['log', 'info', 'warn', 'error', 'debug'];

export const fireBridgeEvent = (eventId, msg) => {
const detail = cloneInto ? cloneInto(msg, document) : msg;
const evtMain = new SafeCustomEvent(eventId, { __proto__: null, detail });
Expand Down
17 changes: 15 additions & 2 deletions src/injected/web/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import bridge, { addHandlers } from './bridge';
import store from './store';
import { GM_API } from './gm-api';
import { makeGmApiWrapper } from './gm-api-wrapper';
import './gm-values';
import './notifications';
import './requests';
import './tabs';
import { bindEvents } from '../util';
import { bindEvents, CONSOLE_METHODS } from '../util';
import { safeConcat } from './util';

// Make sure to call safe::methods() in code that may run after userscripts

const toRun = createNullObj();

export default function initialize(invokeHost) {
export default function initialize(invokeHost, console) {
if (PAGE_MODE_HANDSHAKE) {
window::on(PAGE_MODE_HANDSHAKE + '*', e => {
e = e::getDetail();
Expand All @@ -25,13 +27,24 @@ export default function initialize(invokeHost) {
this[id] = VAULT;
},
});
/* Can't use a detached `console` in Chrome 109+ due to https://crrev.com/1063194 */
if (!IS_FIREFOX) {
for (const m of CONSOLE_METHODS) {
logging[m] = (...args) => bridge.post('Log', [m, args]);
}
/** @this {GMContext} */
GM_API.bound.GM_log = function (...args) {
bridge.post('Log', ['log', safeConcat([`[${this.script.displayName}]`], args)]);
};
}
} else {
bridge.mode = CONTENT;
bridge.post = (cmd, data, node) => {
invokeHost({ cmd, data, node }, CONTENT);
};
global.chrome = undefined;
global.browser = undefined;
logging = console; // eslint-disable-line no-global-assign
return (cmd, data, realm, node) => {
if (process.env.DEBUG) console.info('[bridge.guest.content] received', { cmd, data, node });
bridge.onHandle({ cmd, data, node });
Expand Down
2 changes: 1 addition & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ declare interface GMContext {
id: number;
resCache: StringMap;
resources: StringMap;
script: VMScript;
script: VMInjection.Script;
}

/**
Expand Down

0 comments on commit 386d596

Please sign in to comment.