-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
30 lines (29 loc) · 1.25 KB
/
Copy pathbackground.js
File metadata and controls
30 lines (29 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// background.js - open inspector.html in a dedicated 800x600 window when the action is clicked
chrome.action.onClicked.addListener(() => {
// When the user clicks the action, capture the active tab's URL (activeTab
// grants temporary access) and pass it to the inspector page so it can
// prefill and auto-parse.
try {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
const tabUrl = (tabs && tabs[0] && tabs[0].url) ? tabs[0].url : '';
// Encode the URL as base64 so we can safely pass the exact original
// string via a query parameter without performing URL-decoding.
function b64EncodeUnicode(str) {
// from MDN: https://developer.mozilla.org/en-US/docs/Glossary/Base64
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
function toSolidBytes(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
const inspectorUrl = chrome.runtime.getURL('inspector.html') + (tabUrl ? ('?source_b64=' + b64EncodeUnicode(tabUrl)) : '');
chrome.windows.create({
url: inspectorUrl,
type: 'popup',
width: 800,
height: 600
});
});
} catch (e) {
console.error('Failed to open inspector window', e);
}
});