Skip to content

Instantly share code, notes, and snippets.

@benslv
Created March 3, 2021 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benslv/05278992c4ff2a94d5d3436451954e1c to your computer and use it in GitHub Desktop.
Save benslv/05278992c4ff2a94d5d3436451954e1c to your computer and use it in GitHub Desktop.
Access current tab URL from a React-based broswer extension
/*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