0

So I got a working Chrome Extension that listens for clicks on a webpage and when one of the correct buttons is clicked, logs some text. Now, instead of logging the text, I want to call a Apps Script function that writes the text to a Google Spreadsheet.

My problem now is I really dont understand the whole authentication process. I tried following this tutorial: https://hawksey.info/blog/2017/05/using-the-google-apps-script-execution-api-in-chrome-extensions/

But all I get are cors- or 401-errors.

The Chrome Extension manifest.json:

{
    "name": "Test",
    "version": "1.0.0",
    "description": "test",
    "manifest_version": 3,
    "author": "Munrador",
    "permissions": [
        "activeTab",
        "identity"
    ],
    "content_scripts": [
        {
            "matches": [
                "https://myWebsite"
            ],
            "js": [
                "script.js"
            ]
        }
    ],
    "oauth2": {
        "client_id": "",
        "scopes": [
            "https://www.googleapis.com/auth/spreadsheets"
        ]
    },
    "key": ""
}

script.js:

document.body.addEventListener('click', async e => {
    if (e.target.className === 'fa-solid fa-circle-plus') {
        const script_id = '';
        let text = e.path[4].children[2].children[0].innerText;

        console.log(text);

        await fetch('https://script.googleapis.com/v1/scripts/' + script_id + ':run', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ function: 'test', parameters: [text], devMode: true })
        });
    }
});

I hope someone has done something similar and can help me with this.

MunraX
  • 1
  • 1
  • 1
    Do it in the background script, [example](https://stackoverflow.com/a/55292071/). – wOxxOm Mar 25 '22 at 15:36
  • @wOxxOm Well this fixed the erros but the function still doesnt get executet – MunraX Mar 25 '22 at 16:14
  • Go look at the example wOxxOm linked you, you have to send a message from the content script to the background script on the click of the button. – relentless Mar 25 '22 at 18:09
  • @MunraX If your goal is to populate a spreadsheet, why not call the Sheet API directly instead of going through Apps Script as an intermediary? – TheAddonDepot Mar 26 '22 at 03:50

1 Answers1

0

Check out this page about it: https://hawksey.info/blog/2017/05/using-the-google-apps-script-execution-api-in-chrome-extensions/

It seems you need to deploy as an API executable and do all this stuff in GCP. It is on an older version but not much has changed.

Instead of pressing publish you want the deploy button up the top

Screenshot shows publish menu in old apps script

Inglan
  • 51
  • 5