Skip to content

How can I get the url of the host page in the content script iframe? #78

Closed Answered by mubaidr
missingno-io asked this question in Q&A
Discussion options

You must be logged in to vote

You can send message to background js, which will return the url of this tab?

Content Script or Iframe:

const { url } = await chrome.runtime.sendMessage({ action: 'getTabUrl' })

Backgroundjs:

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
	if (request.action === 'getTabUrl') {
		const tabId = sender.tab.id
		chrome.tabs.get(tabId, (tab) => {
			sendResponse({ url: tab.url })
		})
		return true // Keeps the message channel open for sendResponse
	}
})

I would recommend doing maximum work in background js, this is where all chrome APIs are available, it has the most permissions/ access. USe content script/ iframe for user interface purpose.

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@missingno-io
Comment options

@missingno-io
Comment options

@mubaidr
Comment options

Answer selected by missingno-io
@missingno-io
Comment options

@mubaidr
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants