I'm writing a Chrome extension and, in one part of it, I need to get the current tab's title and URL when a button on the popup page is clicked.
I've worked with Chrome's message passing system before and, after much effort, managed to get it to work, on many occasions. However, I've never had to use them with popup pages and, from what I've read, it's much more difficult to do.
The timeline I've managed to figure out so far is this:
popup.html
/ popup.js
: Button is clicked
popup.html
/ popup.js
: Request / message is sent to the content script
contentScript.js
: Request / message is received from the popup page
contentScript.js
: The title and URL of the current tab are stored in a variable
contentScript.js
: The 2 variables are sent as a stringified response
popup.html
/ popup.js
: The 2 variables are parsed from the response
Usually I'd be able to figure this out but, I've read a few things that have thrown a spanner in the works, such as:
chrome.tabs.getSelected
can only be used in a background page / script. Does this mean that content scripts don't need to be used at all?
- Messages cannot be sent directly from the content script to the popup page, they have to go via a background page
- A popup window has to be confirmed as existing before a message can be passed to it (although, I think I know how to do this)
I already found Chrome's message passing system difficult enough but, this has totally confused me. Hence, this post.
question from:
https://stackoverflow.com/questions/13359421/chrome-extension-get-current-tab-from-popup 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…