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

(e.adapter || a.adapter) is not a function in background script

Toniboy1 opened this issue · comments

Hi, I'm experiencing issues using registerMessageHandler() in my background script.

I'm using webpack.

src/index.js

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

chrome.runtime.onMessageExternal.addListener(
    function (request, sender, sendResponse) {
        console.log('received');
        localRequest(`https://jsonplaceholder.typicode.com/todos/1`, {
        }).then((response) => {
            console.log('sending back')
            console.log(response)
            sendResponse(response.data)
            return true;
        }).catch((e) => {
            console.error(e);
            sendResponse({ result: 'error', message: e.message });
            return false;
        });
    });

const localRequest = async (endpoint, headers) => {
    return await new Promise((resolve, reject) => {
        axios.get(endpoint, {
            headers: headers
        }).then((response) => {
            resolve(response.data)
        }).catch((e) => {
            reject({ result: 'error', message: e.message }); //(e.adapter || a.adapter) is not a function
        });
    })
}

Here is my Manifest

{
    "name": "My additions",
    "description": "Extra communication",
    "version": "1.0",
    "manifest_version": 3,
    "background": {
        "service_worker": "main.js"
    },
    "permissions": [
        "storage",
        "activeTab",
        "scripting"
    ],
    "host_permissions": [
        "*://*/*"
    ],
    "externally_connectable": {
        "matches": [
            "*://*.domain.xyz/*"
        ]
    }
}

Thank you for your consideration :)

I downgraded to Manifest 2 and it seems to work

In your example you're not passing the adapter to axios when making the request.

Also I haven't tried with manifest V3 yet (I will do some testing soon), so for now consider this package to be only compatible with manifest V2.

In your example you're not passing the adapter to axios when making the request.

Also I haven't tried with manifest V3 yet (I will do some testing soon), so for now consider this package to be only compatible with manifest V2.

that request looks like its inside the background script. Wouldn't the adapter be only for axios calls outside of the background script? else it's just sending message to itself