-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to require packages such as Axios in contentscript ? #1
Comments
Hey Robin, glad to hear it's helpful! I have only used additional packages directly in the components. I haven't had a need to use them in a content script before, so I can't offer too much help in that regards. If adding it to a component instead of the content script will work for your project, then you can import it into Here is an example of the import { useEffect, useState } from "react";
import Home from "./components/Home";
import Settings from "./components/Settings";
import axios from "axios";
import './main.css';
function App() {
const [page, setPage] = useState('home')
const [wx, setWx] = useState({})
const [units, setUnits] = useState('celsius')
const [city, setCity] = useState('Ottawa')
useEffect(() => {
chrome.storage.local.get(['units'], function(result) {
result.units && setUnits(result.units.toLowerCase())
})
}, [])
useEffect(() => {
chrome.storage.local.get(['city'], function(result) {
result.city && setCity(result.city)
})
}, [])
useEffect(() => {
getWx()
}, [])
const getWx = () => {
const url = `http://localhost:3000/weather?city=${city}&forecast=current`
chrome.runtime.sendMessage({
"id": "getWeather",
"url": url
})
axios
.get(url)
.then(function (response) {
setWx(response.data)
chrome.runtime.sendMessage({
"id": "getWeather",
"data": response,
"url": url
})
})
.catch(function (error) {
chrome.runtime.sendMessage({
"id": "getWeather",
"data": error
})
})
}
return (
<>
{page === 'home' && <Home setPage={setPage} wx={wx} setUnits={setUnits} units={units} city={city} />}
{page === 'settings' && <Settings setPage={setPage} units={units} setUnits={setUnits} />}
</>
);
}
export default App; |
Hey @sandypockets ! In fact, I need to get some links on the page and ContentScript is the way to access it. I absolutely need to send those data in a database. Maybe I can pass it into a component and then send them. I need to use the Supabase package to do that. Cheers ! |
Alternatively, have you tried using the JavaScript Fetch API instead of Axios? That way you wouldn't need to require a package, and the Fetch API docs suggest it is available to Service Workers. Here are a few resources:
Axios aside, if you need to get data from one part of your extension to another, I find the easiest way to do it is with message passing. Hope this helps! |
Hey Sandy, thank you very much for providing this template.
I just created a contentscript.js, declared it into the manifest and tested it (it's working with a simple console.log).
Cheers !
The text was updated successfully, but these errors were encountered: