Last active
May 5, 2023 03:38
-
-
Save kptdobe/21988a5b7a686ceb415b251084c414e2 to your computer and use it in GitHub Desktop.
Chrome Extension: console log in page console, not in extension console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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