Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Manifest V3 on Chrome #166

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions flow-typed/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ declare var chrome: {
popup: string,
}) => void,
},
action: {
setIcon: (options: {
tabId: number,
path: { [key: string]: string },
}) => void,
setPopup: (options: {
tabId: number,
popup: string,
}) => void,
},
runtime: {
getURL: (path: string) => string,
sendMessage: (config: Object) => void,
Expand Down
23 changes: 12 additions & 11 deletions shells/browser/chrome/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"manifest_version": 2,
"manifest_version": 3,
"name": "Relay Developer Tools",
"description": "Adds Relay debugging tools to the Chrome Developer Tools.",
"version": "0.9.17",
Expand All @@ -12,7 +12,7 @@
"48": "icons/enabled48.png",
"128": "icons/enabled128.png"
},
"browser_action": {
"action": {
"default_icon": {
"16": "icons/disabled16.png",
"32": "icons/disabled32.png",
Expand All @@ -22,22 +22,23 @@
"default_popup": "popups/disabled.html"
},
"devtools_page": "main.html",
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'"
},
"web_accessible_resources": [
"main.html",
"panel.html",
"build/backend.js",
"build/renderer.js"
{
"resources": ["main.html", "panel.html", "build/*.js"],
"matches": ["<all_urls>"]
}
],
"background": {
"scripts": ["build/background.js"],
"persistent": false
"service_worker": "build/background.js"
},
"permissions": ["file:///*", "http://*/*", "https://*/*", "webNavigation"],
"permissions": ["webNavigation", "scripting", "tabs"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["build/injectGlobalHook.js"],
"js": ["build/injectGlobalHook.js", "build/contentScript.js"],
"run_at": "document_start"
}
]
Expand Down
23 changes: 11 additions & 12 deletions shells/browser/shared/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ function isNumeric(str: string): boolean {
}

function installContentScript(tabId: number) {
chrome.tabs.executeScript(
tabId,
{ file: '/build/contentScript.js' },
function() {}
);
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['/build/contentScript.js'],
});
}

function doublePipe(one: any, two: any) {
Expand All @@ -70,18 +69,18 @@ function doublePipe(one: any, two: any) {
}

function setIconAndPopup(relayBuildType: string, tabId: number) {
chrome.browserAction.setIcon({
chrome.action.setIcon({
tabId: tabId,
path: {
'16': `icons/${relayBuildType}16.png`,
'32': `icons/${relayBuildType}32.png`,
'48': `icons/${relayBuildType}48.png`,
'128': `icons/${relayBuildType}128.png`,
'16': chrome.runtime.getURL(`icons/${relayBuildType}16.png`),
'32': chrome.runtime.getURL(`icons/${relayBuildType}32.png`),
'48': chrome.runtime.getURL(`icons/${relayBuildType}48.png`),
'128': chrome.runtime.getURL(`icons/${relayBuildType}128.png`),
},
});
chrome.browserAction.setPopup({
chrome.action.setPopup({
tabId: tabId,
popup: 'popups/' + relayBuildType + '.html',
popup: chrome.runtime.getURL(`popups/${relayBuildType}.html`),
});
}

Expand Down
24 changes: 5 additions & 19 deletions shells/browser/shared/src/injectGlobalHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@
/* global chrome */

import nullthrows from 'nullthrows';
import { installHook } from 'src/hook';

function injectCode(code: string) {
function injectCode() {
const script = document.createElement('script');
script.textContent = code;

// This script runs before the <head> element is created,
// so we add the script to <html> instead.
nullthrows(document.documentElement).appendChild(script);
nullthrows(script.parentNode).removeChild(script);
script.src = chrome.runtime.getURL('build/injectedRelayDevToolsDetector.js');
document.documentElement.appendChild(script);
script.remove();
}

let lastDetectionResult;
Expand Down Expand Up @@ -59,14 +55,4 @@ window.addEventListener('pageshow', function(evt) {
chrome.runtime.sendMessage(lastDetectionResult);
});

const detectRelay = `
window.__RELAY_DEVTOOLS_HOOK__.on('environment', function(evt) {
window.postMessage({
source: 'relay-devtools-detector',
}, '*');
});
`;

// Inject a `__RELAY_DEVTOOLS_HOOK__` global so that Relay can detect that the
// devtools are installed (and skip its suggestion to install the devtools).
injectCode(';(' + installHook.toString() + '(window))' + detectRelay);
injectCode();
15 changes: 15 additions & 0 deletions shells/browser/shared/src/injectedRelayDevToolsDetector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { installHook } from 'src/hook';

// Inject a `__RELAY_DEVTOOLS_HOOK__` global so that Relay can detect that the
// devtools are installed (and skip its suggestion to install the devtools).
(function() {
installHook(window);
window.__RELAY_DEVTOOLS_HOOK__.on('environment', function(evt) {
window.postMessage(
{
source: 'relay-devtools-detector',
},
'*'
);
});
})();
1 change: 1 addition & 0 deletions shells/browser/shared/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = {
main: './src/main.js',
panel: './src/panel.js',
renderer: './src/renderer.js',
injectedRelayDevToolsDetector: './src/injectedRelayDevToolsDetector.js',
},
output: {
path: __dirname + '/build',
Expand Down