1 year ago
#374876
Paul
Importing `day.js` doesn't work in my extension despite being usable in the console
I'm trying to use the day.js
library in my extension. Data is passed using browser.runtime.sendMessage()
from my content script to my background script, which is where I use day.js
.
Oddly enough, Error: dayjs is not a function
appears in the console of the webpage where the content script runs (e.g. google.com
rather than about:devtools-toolbox?id=my-extension&type=extension
). I'm using Firefox.
Furthermore, I can run dayjs()
with no problems in the console of about:devtools-toolbox
. Why doesn't it work in my background script? And why does the error appear in the content script's context?
background.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- Background page is necessary for supporting ES6 modules -->
<script type="module" src="background.js"></script>
</head>
</html>
background.js
:
import * as dayjs from "./dayjs@1.11.0/dayjs.min.js";
import * as relativeTime from "./dayjs@1.11.0/relativeTime.js";
async function calculateAge(datetimeString) {
const datetime = dayjs(datetimeString);
dayjs.extend(relativeTime);
const age = datetimeCreated.fromNow();
return age;
}
browser.runtime.onMessage.addListener(calculateAge);
content.js
:
async function getAge(datetimeString) {
const age = await browser.runtime.sendMessage(datetimeString);
return age;
}
manifest.json
:
"background": {
"page": "background.html"
},
"content_scripts": [
{
"js": ["content.js"],
"matches": ["https://*.google.com/*"]
}
],
javascript
html
firefox-addon
firefox-addon-webextensions
dayjs
0 Answers
Your Answer