Skip to content

Instantly share code, notes, and snippets.

@dance2die
Last active Apr 11, 2021
Embed
What would you like to do?
chrome.tabs.query(
{ active: true, windowId: chrome.windows.WINDOW_ID_CURRENT },
function(tabs) {
const { id: tabId } = tabs[0].url;
let code = `document.querySelector('h1')`;
// http://infoheap.com/chrome-extension-tutorial-access-dom/
chrome.tabs.executeScript(tabId, { code }, function (result) {
// result has the return value from `code`
});
}
);
@Lordoomer
Copy link

Lordoomer commented Apr 11, 2021

I'm getting a Cannot read property 'query' of undefined error, here is the code I'm using it with please help me, the stackoverflow says that it has to be in the popup script and it is in it so I'm at loss as to why ti won't work...
popup.js
let changeColor = document.getElementById("changeColor");

chrome.storage.sync.get("color", ({ color }) => {
changeColor.style.backgroundColor = color;
});
changeColor.addEventListener("click", async () => {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

chrome.scripting.executeScript({
target: { tabId: tab.id },
function: CheckCryptoCurrencyValue,
});
});
function CheckCryptoCurrencyValue(){
chrome.storage.sync.get("color", ({ color }) => {
document.body.style.backgroundColor = color;
});
chrome.tabs.query(
{ active: true, windowId: chrome.windows.WINDOW_ID_CURRENT },
function(tabs) {
const { id: tabId } = tabs[0].url;

	let code = `document.querySelector('h1')`;
	chrome.tabs.executeScript(tabId, { code }, function (result) {
			console.log(result +' Bitcoins:'  + result[0].innerHTML + '$ (CAD)');
	  // result has the return value from `code`
	});
  }
);

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment