Created
March 3, 2021 14:12
-
-
Save benslv/05278992c4ff2a94d5d3436451954e1c to your computer and use it in GitHub Desktop.
Access current tab URL from a React-based broswer extension
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
/*global browser*/ | |
import React, { useState } from "react"; | |
import "./App.css"; | |
function App() { | |
const [currentURL, setCurrentURL] = useState(""); | |
const grabURL = async () => { | |
const tabs = await browser.tabs.query({ | |
active: true, | |
currentWindow: true, | |
}); | |
setCurrentURL(tabs[0].url); | |
}; | |
return ( | |
<div className="App"> | |
<h1>Hello, world!</h1> | |
<p> | |
Just testing that I can render custom content and stuff in my | |
browser extension. | |
</p> | |
<button onClick={grabURL}>Get current URL</button> | |
<span>{currentURL}</span> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment