Skip to content

Instantly share code, notes, and snippets.

@kptdobe
Last active May 5, 2023 03:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kptdobe/21988a5b7a686ceb415b251084c414e2 to your computer and use it in GitHub Desktop.
Save kptdobe/21988a5b7a686ceb415b251084c414e2 to your computer and use it in GitHub Desktop.
Chrome Extension: console log in page console, not in extension console
// returns current tab
const getCurrentTab = async () => {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
return tab;
}
// log to current page console
const log = async (...arguments) => {
const tab = await getCurrentTab();
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (...arguments) => {
console.log('[from extension]', ...arguments);
},
args: arguments,
});
}
// logs go to extension console
console.log('log in extension console');
// logs go to page console
log('log in page console');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment