I'm having fun with Google Chrome extension, and I just want to know how can I store the URL of the current tab in a variable?
相关问题
- Browsers render final comma of right-to-left (rtl)
- Chrome dev tools exact computed value for CSS rule
- Change Chromium icon and “chrome” text to custom i
- debugging webgl in chrome
- Shadow DOM CSS Styling from outside is not working
相关文章
- Is there a way to hide the new HTML5 spinbox contr
- Google Chrome Cache
- how to download a file on Chrome without auto rena
- Do all browsers on iOS use WKWebview or UIWebVIew?
- Why does Google Chrome NOT use cached pages when I
- Calling Chrome web browser from the webbrowser.get
- Progressive web app(PWA) vs Electron vs Browser ex
- Stop automatic scrolling on page content change
Other answers assume you want to know it from a popup or background script.
In case you want to know the current URL from a content script, the standard JS way applies:
You can use properties of
window.location
to access individual parts of the URL, such as host, protocol or path.A friend answers to my question.
First, you've to set the permissions for the tab API :
And to store the URL :
Update: The
chrome.tabs.getSelected()
method is now deprecated.The recommended way of getting the URL of the current tab is to use
chrome.tabs.query()
.An example usage:
This requires that you request access to the
chrome.tabs
API in your extension manifest:It's important to note that the definition of your "current tab" may differ depending on your extension's needs.
Setting
lastFocusedWindow: true
in the query is appropriate when you want to access the current tab in the user's focused window (typically the topmost window).Setting
currentWindow: true
allows you to get the current tab in the window where your extension's code is currently executing. For example, this might be useful if your extension creates a new window / popup (changing focus), but still wants to access tab information from the window where the extension was run.I chose to use
lastFocusedWindow: true
in this example, because Google calls out cases in whichcurrentWindow
may not always be present.You are free to further refine your tab query using any of the properties defined here: chrome.tabs.query
The problem is that chrome.tabs.getSelected is asynchronous. This code below will generally not work as expected. The value of 'tablink' will still be undefined when it is written to the console because getSelected has not yet invoked the callback that resets the value:
The solution is to wrap the code where you will be using the value in a function and have that invoked by getSelected. In this way you are guaranteed to always have a value set, because your code will have to wait for the value to be provided before it is executed.
Try something like:
Hi here is an Google Chrome Sample which emails the current Site to an friend. The Basic idea behind is what you want...first of all it fetches the content of the page (not interessting for you)...afterwards it gets the URL (<-- good part)
Additionally it is a nice working code example, which i prefer motstly over reading Documents.
Can be found here: Email this page
For those using the
context menu api
, the docs are not immediately clear on how to obtain tab information.https://developer.chrome.com/extensions/contextMenus