Skip to content

Commit 3acea5e

Browse files
committed
Updated version 1.1.0 and upgraded various devDependencies.
Improved type annotations. Enhanced event listener parameter types. Fixed potential null reference in report.js when clearing the report table. Added error handling for script and CSS injection. Updated event parameter types in toolbar.js for better clarity.
1 parent db1aa0c commit 3acea5e

File tree

10 files changed

+754
-646
lines changed

10 files changed

+754
-646
lines changed

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export default [
6565
Object.entries(globals.serviceworker).map(([key]) => [key, 'off']),
6666
),
6767
console: true,
68+
crypto: true,
6869
DEVMODE: false,
6970
VERSION: false,
7071
ViewabilityInsights: true,

package-lock.json

Lines changed: 693 additions & 595 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
{
22
"name": "admanager-viewability-insights",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "",
55
"author": "Markus Bordihn <mbordihn@google.com>",
66
"private": true,
77
"license": "MIT",
88
"devDependencies": {
9-
"@material-design-icons/font": "^0.14.13",
10-
"@typescript-eslint/eslint-plugin": "^8.4.0",
11-
"@typescript-eslint/parser": "^8.4.0",
12-
"copy-webpack-plugin": "^12.0.2",
9+
"@material-design-icons/font": "^0.14.15",
10+
"@typescript-eslint/eslint-plugin": "^8.46.2",
11+
"@typescript-eslint/parser": "^8.46.2",
12+
"copy-webpack-plugin": "^13.0.1",
1313
"css-loader": "^7.1.2",
14-
"eslint": "^9.9.1",
15-
"eslint-config-prettier": "^9.1.0",
16-
"eslint-plugin-compat": "^6.0.1",
17-
"eslint-plugin-jsdoc": "^50.2.2",
18-
"eslint-plugin-prettier": "^5.2.1",
14+
"eslint": "^9.38.0",
15+
"eslint-config-prettier": "^10.1.8",
16+
"eslint-plugin-compat": "^6.0.2",
17+
"eslint-plugin-jsdoc": "^61.1.5",
18+
"eslint-plugin-prettier": "^5.5.4",
1919
"file-loader": "^6.2.0",
20-
"html-webpack-plugin": "^5.6.3",
21-
"mini-css-extract-plugin": "^2.9.2",
20+
"html-webpack-plugin": "^5.6.4",
21+
"mini-css-extract-plugin": "^2.9.4",
2222
"style-loader": "^4.0.0",
23-
"terser-webpack-plugin": "^5.3.10",
24-
"webpack": "^5.96.1",
25-
"webpack-cli": "^5.1.4"
23+
"terser-webpack-plugin": "^5.3.14",
24+
"webpack": "^5.102.1",
25+
"webpack-cli": "^6.0.1"
2626
},
2727
"scripts": {
2828
"test": "npm run build:dev & npm run build",

src/content_script/content_script.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class ContentScript {
7171
}
7272

7373
/**
74-
* @param {*} message Message
74+
* @param {object} message Message
7575
*/
7676
handlePortMessage(message) {
7777
if (message && message.token) {
@@ -124,9 +124,10 @@ class ContentScript {
124124
if (
125125
event.source !== window ||
126126
!event.data ||
127-
typeof event.data.token == 'undefined' ||
127+
typeof event.data !== 'object' ||
128+
!event.data.token ||
128129
!this.token ||
129-
event.data.token != this.token
130+
event.data.token !== this.token
130131
) {
131132
return;
132133
}

src/gpt/gpt_integration_script.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export default class ViewabilityInsights {
7474

7575
/**
7676
* @param {string} name Event Name
77-
* @param {Function} listener Event Listener
77+
* @param {(...args: unknown[]) => void} listener Event Listener
7878
*/
7979
addEventListener(name, listener) {
8080
try {
@@ -518,19 +518,15 @@ export default class ViewabilityInsights {
518518
) {
519519
const overlayRect = overlay.getBoundingClientRect();
520520
const iframeRect = iframeElement.getBoundingClientRect();
521-
if (overlayRect && iframeRect) {
522-
if (
523-
Math.abs(Math.round(overlayRect.top) - Math.round(iframeRect.top)) >
524-
1
525-
) {
526-
overlay.style.top = iframeRect.top - overlayRect.top + 'px';
521+
const parentRect = overlay.parentElement.getBoundingClientRect();
522+
if (overlayRect && iframeRect && parentRect) {
523+
const topDiff = Math.round(iframeRect.top - parentRect.top);
524+
const leftDiff = Math.round(iframeRect.left - parentRect.left);
525+
if (Math.abs(topDiff) > 1) {
526+
overlay.style.top = topDiff + 'px';
527527
}
528-
if (
529-
Math.abs(
530-
Math.round(overlayRect.left) - Math.round(iframeRect.left),
531-
) > 1
532-
) {
533-
overlay.style.left = iframeRect.left - overlayRect.left + 'px';
528+
if (Math.abs(leftDiff) > 1) {
529+
overlay.style.left = leftDiff + 'px';
534530
}
535531
}
536532
}

src/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Ad Manager Viewability Insights",
33
"description": "A DevTools extension for inspecting the viewability of Ad Manager Ads",
4-
"version": "0.8.0",
4+
"version": "1.1.0",
55
"manifest_version": 3,
66
"icons": {
77
"32": "logo_48px.png"

src/panel/message_handler.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class MessageHandler {
4141
this.sender = null;
4242
this.statusbar = null;
4343
this.tabId = null;
44-
this.token = Math.random().toString(36).substring(2);
44+
this.token = crypto.randomUUID();
4545
this.toolbar = null;
4646
}
4747

@@ -82,20 +82,22 @@ class MessageHandler {
8282
* @see https://developer.chrome.com/docs/extensions/reference/runtime/#event-onConnect
8383
*/
8484
handleConnect(port) {
85-
this.port = port;
86-
this.sender = port.sender;
87-
this.tabId = this.sender.tab.id;
88-
8985
// Ignore events from other tabs.
90-
if (this.tabId != chrome.devtools.inspectedWindow.tabId) {
86+
if (
87+
!port.sender?.tab?.id ||
88+
port.sender?.tab?.id !== chrome.devtools.inspectedWindow.tabId
89+
) {
9190
return;
9291
}
9392

9493
// Ignore events from other names.
95-
if (port.name != this.connectionName) {
94+
if (port.name !== this.connectionName) {
9695
return;
9796
}
9897

98+
this.port = port;
99+
this.sender = port.sender;
100+
this.tabId = this.sender.tab.id;
99101
this.connected = false;
100102

101103
// Add Message Listener
@@ -107,7 +109,7 @@ class MessageHandler {
107109
}
108110

109111
/**
110-
* @param {Function} message Callback function for messages.
112+
* @param {object} message Callback function for messages.
111113
* @see https://developer.chrome.com/docs/extensions/reference/runtime/#event-onMessage
112114
*/
113115
handleMessage(message) {

src/panel/report.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,17 @@ class Report {
6363
*/
6464
clear() {
6565
const reportTable = document.querySelector('#report > tbody');
66-
reportTable.parentNode.replaceChild(
67-
document.createElement('tbody'),
68-
reportTable,
69-
);
66+
if (reportTable?.parentNode) {
67+
reportTable.parentNode.replaceChild(
68+
document.createElement('tbody'),
69+
reportTable,
70+
);
71+
}
7072
this.slotCounter = 0;
7173
}
7274

7375
/**
74-
* @param {*} slot Slot
76+
* @param {object} slot Slot
7577
* @returns {HTMLElement} row
7678
*/
7779
addSlot(slot) {
@@ -149,7 +151,7 @@ class Report {
149151
}
150152

151153
/**
152-
* @param {*} slot Slot
154+
* @param {object} slot Slot
153155
*/
154156
updateSlot(slot) {
155157
if (!slot) {
@@ -216,7 +218,7 @@ class Report {
216218
}
217219

218220
/**
219-
* @param {*} event Event
221+
* @param {MouseEvent} event Event
220222
*/
221223
scrollIntoView(event) {
222224
if (event.target && event.target.innerText) {
@@ -228,8 +230,8 @@ class Report {
228230
* @param {HTMLElement} row Row
229231
* @param {number} index Index
230232
* @param {string} name Name
231-
* @param {*} value Value
232-
* @param {Function?} clickHandler Click handler
233+
* @param {string|number|boolean} value Value
234+
* @param {((event: MouseEvent) => void)|null} clickHandler Click handler
233235
* @private
234236
*/
235237
insertCell_(row, index, name, value, clickHandler = null) {

src/panel/script_handler.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class ScriptHandler {
3838
world: 'MAIN',
3939
},
4040
(injectionResults) => {
41+
if (chrome.runtime.lastError) {
42+
console.error('Script injection failed:', chrome.runtime.lastError);
43+
return;
44+
}
4145
console.log('Inject Script results:', injectionResults);
4246
ScriptHandler.injectStyleSheet(tabId);
4347
ScriptHandler.initScript(tabId, sessionToken);
@@ -56,6 +60,10 @@ class ScriptHandler {
5660
target: { tabId: tabId },
5761
},
5862
(injectionResults) => {
63+
if (chrome.runtime.lastError) {
64+
console.error('CSS injection failed:', chrome.runtime.lastError);
65+
return;
66+
}
5967
console.log('Inject StyleSheet results:', injectionResults);
6068
},
6169
);

src/panel/toolbar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Toolbar {
5555
}
5656

5757
/**
58-
* @param {event} event Event
58+
* @param {MouseEvent} event Event
5959
*/
6060
reloadPage(event) {
6161
if (event && chrome.devtools.inspectedWindow.tabId) {
@@ -73,7 +73,7 @@ class Toolbar {
7373
}
7474

7575
/**
76-
* @param {*} event Event
76+
* @param {MouseEvent} event Event
7777
*/
7878
showViewableOverlay(event) {
7979
console.debug('Show Viewable Overlay', event.currentTarget.checked);

0 commit comments

Comments
 (0)