Skip to content

Instantly share code, notes, and snippets.

@dance2die
Last active July 9, 2022 08:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dance2die/d05924be83c6be1b20ba94206ff2beb0 to your computer and use it in GitHub Desktop.
Save dance2die/d05924be83c6be1b20ba94206ff2beb0 to your computer and use it in GitHub Desktop.
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

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