sybers / axios-chrome-messaging-adapter

Axios adapter forwarding requests to Chrome Extension's background script.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error when I import { registerMessageHandler } in background.js

Nevgup opened this issue · comments

commented

Hello !

I have this content script (in src/index.js) file :

import axios from "../dist/axios-client";


async function productStock(url) {
    const { data } = await axios.get(url)
    let availabilityProduct = data.product
    let status;

    availabilityProduct === "no_stock" ? status = false : status = true

    return status
}

let alarmClock = {
    activateNotification: function(e) {
        chrome.alarms.create("myAlarm", { delayInMinutes: 1, periodInMinutes: 1 });
        window.close();
    },

    deactivateNotification: function(e) {
        chrome.alarms.clear("myAlarm");
        window.close();
    },

    setup: function() {
        var a = document.getElementById('alarmOn');
        a.addEventListener('click', alarmClock.activateNotification);
        var a = document.getElementById('alarmOff');
        a.addEventListener('click', alarmClock.deactivateNotification);
    }
};

document.addEventListener('DOMContentLoaded', function() {
    alarmClock.setup();
});

Here is my dist/axios-client.js :

import axios from "axios"
import { adapter } from 'axios-chrome-messaging-adapter'

const client = axios.create({ adapter })

export default client

and my background.js :

import { registerMessageHandler } from 'axios-chrome-messaging-adapter';
registerMessageHandler();

chrome.alarms.onAlarm.addListener(async() => {
    alert("ping")
   // and here i would like to use a content main script function
});

The problem is I can import anything from the background. When i try to import, I have this error :
image

Someone can help me please ?

Thanks in advance !

Are you using a bundler ? Like Webpack or Rollup ?

The error you get is a JS error unrelated to this package, it's complaining about the use of import outside a module file...

Either add module type to your script tag (<script type="module" src="..."></script>) or use a bundler so the output is a single concatenated JS file.

As this error seems unrelated to axios-chrome-messaging-adapter and there was no recent update on this ticket, I'm closing it for now.

Feel free to add a comment again and I'll re-open it.