Currently, I am facing a persistent error while creating a Google Chrome extension in Manifest V3:
Error: Uncaught TypeError: Cannot read properties of undefined (reading 'id')
Despite extensive research, I keep running into different errors with every solution I attempt. As I am relatively new to Javascript, I suspect this issue might be due to a beginner mistake on my part. Here is the code I have so far:
index.html
<html>
<head>
<link rel="stylesheet" href="styles.css">
<link rel="chart" href="Chart/setup.js">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto:wght@700&display=swap">
<link rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="70372231343042405e5e4448">[email protected]</a>,100..700,0..1,-50..200" />
<script type="js/main.js" src="https://www.gstatic.com/charts/loader.js"></script>
</head>
<body>
<div id="donutchart" style="width: 900px; height: 500px;"></div>
<h4>GOOGLE CALENDAR</h4>
<h5>Time Tracker</h5>
<hr></hr>
</body>
</html>
manifest.json
{
"manifest_version": 3,
"name": "Google Calendar Time Tracker",
"description": "Base Level Extension",
"version": "1.0",
"action": {
"default_popup": "index.html",
"default_icon": "hello_extensions.png"
},
"background": {
"service_worker": "js/main.js"
},
"permissions": [
"scripting",
"tabs",
"https://*/*",
"http://*/*"
]
}
main.js
function getTabId() {
let tabs = chrome.tabs.query({currentWindow: true, active : true});
return tabs[0].tabId;
}
chrome.scripting.executeScript({
target: {tabId: getTabId()},
files : ["chart.js"],
})
.then(() => console.log("Script injected"));
chart.js
google.charts.load("current", { packages: ["corechart"] });
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var options = {
title: 'My Daily Activities',
pieHole: 0.4,
};
var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
chart.draw(data, options);
}
If you have any suggestions for improvements or solutions, kindly share them as I aim to successfully import and display a pi chart using the Google Charts library in my extension.
I've attempted changing 'id' to 'tabId' and experimented with promises, yet none of these approaches worked. The variable 'id'/'tabId' appears unrecognized despite online guides utilizing it. What could I possibly be missing?