Luna McGrove
Luna McGrove

Reputation: 39

Google Chrome Extension - Getting data from website

I'm designing my Google Chrome extension to store a variable created in a completely different website, which means I need to pass the variable over.

This is the code in the script.js of the website:

var editorExtensionId = "extension";

'use strict';
function x(){
    chrome.runtime.sendMessage({n:1});
}
chrome.runtime.onMessage.addListener(function(loginKey){
    console.log(loginKey.n);
});
chrome.action.onClicked.addListener(function(tab){
    chrome.scripting.executeScript({
        target: {tabId: tab.id},
        function: x
    });
});

In the console log, however, it gives me the error "Uncaught TypeError: Cannot read properties of undefined (reading 'addListener') at script.js:68:26"

I was wondering how to fix this.

Upvotes: 1

Views: 1283

Answers (1)

Thi Hoang
Thi Hoang

Reputation: 87

Your extension works on a website but you want some data from another website?

  • Firstly, add permission storage for your manifest.json file to enable Chrome local storage.

  • Then use "Document.querySelector()" or "Document.getElementById()" to take whatever data you need from website 2, write it into chrome storage by "chrome.storage.local.set"

  • Finally at website 1, retrieve the variable you stored by "chrome.storage.local.get"

Upvotes: 1

Related Questions