chrome-extension.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. 'use strict';
  2. const { app, webContents, BrowserWindow } = require('electron');
  3. const { getAllWebContents } = process.electronBinding('web_contents');
  4. const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils');
  5. const { Buffer } = require('buffer');
  6. const fs = require('fs');
  7. const path = require('path');
  8. const url = require('url');
  9. const util = require('util');
  10. // Mapping between extensionId(hostname) and manifest.
  11. const manifestMap = {}; // extensionId => manifest
  12. const manifestNameMap = {}; // name => manifest
  13. const devToolsExtensionNames = new Set();
  14. const generateExtensionIdFromName = function (name) {
  15. return name.replace(/[\W_]+/g, '-').toLowerCase();
  16. };
  17. const isWindowOrWebView = function (webContents) {
  18. const type = webContents.getType();
  19. return type === 'window' || type === 'webview';
  20. };
  21. const isBackgroundPage = function (webContents) {
  22. return webContents.getType() === 'backgroundPage';
  23. };
  24. // Create or get manifest object from |srcDirectory|.
  25. const getManifestFromPath = function (srcDirectory) {
  26. let manifest;
  27. let manifestContent;
  28. try {
  29. manifestContent = fs.readFileSync(path.join(srcDirectory, 'manifest.json'));
  30. } catch (readError) {
  31. console.warn(`Reading ${path.join(srcDirectory, 'manifest.json')} failed.`);
  32. console.warn(readError.stack || readError);
  33. throw readError;
  34. }
  35. try {
  36. manifest = JSON.parse(manifestContent);
  37. } catch (parseError) {
  38. console.warn(`Parsing ${path.join(srcDirectory, 'manifest.json')} failed.`);
  39. console.warn(parseError.stack || parseError);
  40. throw parseError;
  41. }
  42. if (!manifestNameMap[manifest.name]) {
  43. const extensionId = generateExtensionIdFromName(manifest.name);
  44. manifestMap[extensionId] = manifestNameMap[manifest.name] = manifest;
  45. Object.assign(manifest, {
  46. srcDirectory: srcDirectory,
  47. extensionId: extensionId,
  48. // We can not use 'file://' directly because all resources in the extension
  49. // will be treated as relative to the root in Chrome.
  50. startPage: url.format({
  51. protocol: 'chrome-extension',
  52. slashes: true,
  53. hostname: extensionId,
  54. pathname: manifest.devtools_page
  55. })
  56. });
  57. return manifest;
  58. } else if (manifest && manifest.name) {
  59. console.warn(`Attempted to load extension "${manifest.name}" that has already been loaded.`);
  60. return manifest;
  61. }
  62. };
  63. // Manage the background pages.
  64. const backgroundPages = {};
  65. const startBackgroundPages = function (manifest) {
  66. if (backgroundPages[manifest.extensionId] || !manifest.background) return;
  67. let html;
  68. let name;
  69. if (manifest.background.page) {
  70. name = manifest.background.page;
  71. html = fs.readFileSync(path.join(manifest.srcDirectory, manifest.background.page));
  72. } else {
  73. name = '_generated_background_page.html';
  74. const scripts = manifest.background.scripts.map((name) => {
  75. return `<script src="${name}"></script>`;
  76. }).join('');
  77. html = Buffer.from(`<html><body>${scripts}</body></html>`);
  78. }
  79. const contents = webContents.create({
  80. partition: 'persist:__chrome_extension',
  81. type: 'backgroundPage',
  82. sandbox: true,
  83. enableRemoteModule: false
  84. });
  85. backgroundPages[manifest.extensionId] = { html: html, webContents: contents, name: name };
  86. contents.loadURL(url.format({
  87. protocol: 'chrome-extension',
  88. slashes: true,
  89. hostname: manifest.extensionId,
  90. pathname: name
  91. }));
  92. };
  93. const removeBackgroundPages = function (manifest) {
  94. if (!backgroundPages[manifest.extensionId]) return;
  95. backgroundPages[manifest.extensionId].webContents.destroy();
  96. delete backgroundPages[manifest.extensionId];
  97. };
  98. const sendToBackgroundPages = function (...args) {
  99. for (const page of Object.values(backgroundPages)) {
  100. if (!page.webContents.isDestroyed()) {
  101. page.webContents._sendInternalToAll(...args);
  102. }
  103. }
  104. };
  105. // Dispatch web contents events to Chrome APIs
  106. const hookWebContentsEvents = function (webContents) {
  107. const tabId = webContents.id;
  108. sendToBackgroundPages('CHROME_TABS_ONCREATED');
  109. webContents.on('will-navigate', (event, url) => {
  110. sendToBackgroundPages('CHROME_WEBNAVIGATION_ONBEFORENAVIGATE', {
  111. frameId: 0,
  112. parentFrameId: -1,
  113. processId: webContents.getProcessId(),
  114. tabId: tabId,
  115. timeStamp: Date.now(),
  116. url: url
  117. });
  118. });
  119. webContents.on('did-navigate', (event, url) => {
  120. sendToBackgroundPages('CHROME_WEBNAVIGATION_ONCOMPLETED', {
  121. frameId: 0,
  122. parentFrameId: -1,
  123. processId: webContents.getProcessId(),
  124. tabId: tabId,
  125. timeStamp: Date.now(),
  126. url: url
  127. });
  128. });
  129. webContents.once('destroyed', () => {
  130. sendToBackgroundPages('CHROME_TABS_ONREMOVED', tabId);
  131. });
  132. };
  133. // Handle the chrome.* API messages.
  134. let nextId = 0;
  135. ipcMainUtils.handle('CHROME_RUNTIME_CONNECT', function (event, extensionId, connectInfo) {
  136. if (isBackgroundPage(event.sender)) {
  137. throw new Error('chrome.runtime.connect is not supported in background page');
  138. }
  139. const page = backgroundPages[extensionId];
  140. if (!page || page.webContents.isDestroyed()) {
  141. throw new Error(`Connect to unknown extension ${extensionId}`);
  142. }
  143. const tabId = page.webContents.id;
  144. const portId = ++nextId;
  145. event.sender.once('render-view-deleted', () => {
  146. if (page.webContents.isDestroyed()) return;
  147. page.webContents._sendInternalToAll(`CHROME_PORT_DISCONNECT_${portId}`);
  148. });
  149. page.webContents._sendInternalToAll(`CHROME_RUNTIME_ONCONNECT_${extensionId}`, event.sender.id, portId, connectInfo);
  150. return { tabId, portId };
  151. });
  152. ipcMainUtils.handle('CHROME_EXTENSION_MANIFEST', function (event, extensionId) {
  153. const manifest = manifestMap[extensionId];
  154. if (!manifest) {
  155. throw new Error(`Invalid extensionId: ${extensionId}`);
  156. }
  157. return manifest;
  158. });
  159. ipcMainUtils.handle('CHROME_RUNTIME_SEND_MESSAGE', async function (event, extensionId, message) {
  160. if (isBackgroundPage(event.sender)) {
  161. throw new Error('chrome.runtime.sendMessage is not supported in background page');
  162. }
  163. const page = backgroundPages[extensionId];
  164. if (!page || page.webContents.isDestroyed()) {
  165. throw new Error(`Connect to unknown extension ${extensionId}`);
  166. }
  167. return ipcMainUtils.invokeInWebContents(page.webContents, true, `CHROME_RUNTIME_ONMESSAGE_${extensionId}`, event.sender.id, message);
  168. });
  169. ipcMainUtils.handle('CHROME_TABS_SEND_MESSAGE', async function (event, tabId, extensionId, message) {
  170. const contents = webContents.fromId(tabId);
  171. if (!contents) {
  172. throw new Error(`Sending message to unknown tab ${tabId}`);
  173. }
  174. const senderTabId = isBackgroundPage(event.sender) ? null : event.sender.id;
  175. return ipcMainUtils.invokeInWebContents(contents, true, `CHROME_RUNTIME_ONMESSAGE_${extensionId}`, senderTabId, message);
  176. });
  177. const getLanguage = () => {
  178. return app.getLocale().replace(/-.*$/, '').toLowerCase();
  179. };
  180. const getMessagesPath = (extensionId) => {
  181. const metadata = manifestMap[extensionId];
  182. if (!metadata) {
  183. throw new Error(`Invalid extensionId: ${extensionId}`);
  184. }
  185. const localesDirectory = path.join(metadata.srcDirectory, '_locales');
  186. const language = getLanguage();
  187. try {
  188. const filename = path.join(localesDirectory, language, 'messages.json');
  189. fs.accessSync(filename, fs.constants.R_OK);
  190. return filename;
  191. } catch {
  192. const defaultLocale = metadata.default_locale || 'en';
  193. return path.join(localesDirectory, defaultLocale, 'messages.json');
  194. }
  195. };
  196. ipcMainUtils.handle('CHROME_GET_MESSAGES', async function (event, extensionId) {
  197. const messagesPath = getMessagesPath(extensionId);
  198. return fs.promises.readFile(messagesPath);
  199. });
  200. const validStorageTypes = new Set(['sync', 'local']);
  201. const getChromeStoragePath = (storageType, extensionId) => {
  202. if (!validStorageTypes.has(storageType)) {
  203. throw new Error(`Invalid storageType: ${storageType}`);
  204. }
  205. if (!manifestMap[extensionId]) {
  206. throw new Error(`Invalid extensionId: ${extensionId}`);
  207. }
  208. return path.join(app.getPath('userData'), `/Chrome Storage/${extensionId}-${storageType}.json`);
  209. };
  210. ipcMainUtils.handle('CHROME_STORAGE_READ', async function (event, storageType, extensionId) {
  211. const filePath = getChromeStoragePath(storageType, extensionId);
  212. try {
  213. return await fs.promises.readFile(filePath, 'utf8');
  214. } catch (error) {
  215. if (error.code === 'ENOENT') {
  216. return null;
  217. } else {
  218. throw error;
  219. }
  220. }
  221. });
  222. ipcMainUtils.handle('CHROME_STORAGE_WRITE', async function (event, storageType, extensionId, data) {
  223. const filePath = getChromeStoragePath(storageType, extensionId);
  224. try {
  225. await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
  226. } catch {
  227. // we just ignore the errors of mkdir
  228. }
  229. return fs.promises.writeFile(filePath, data, 'utf8');
  230. });
  231. const isChromeExtension = function (pageURL) {
  232. const { protocol } = url.parse(pageURL);
  233. return protocol === 'chrome-extension:';
  234. };
  235. const assertChromeExtension = function (contents, api) {
  236. const pageURL = contents._getURL();
  237. if (!isChromeExtension(pageURL)) {
  238. console.error(`Blocked ${pageURL} from calling ${api}`);
  239. throw new Error(`Blocked ${api}`);
  240. }
  241. };
  242. ipcMainUtils.handle('CHROME_TABS_EXECUTE_SCRIPT', async function (event, tabId, extensionId, details) {
  243. assertChromeExtension(event.sender, 'chrome.tabs.executeScript()');
  244. const contents = webContents.fromId(tabId);
  245. if (!contents) {
  246. throw new Error(`Sending message to unknown tab ${tabId}`);
  247. }
  248. let code, url;
  249. if (details.file) {
  250. const manifest = manifestMap[extensionId];
  251. code = String(fs.readFileSync(path.join(manifest.srcDirectory, details.file)));
  252. url = `chrome-extension://${extensionId}${details.file}`;
  253. } else {
  254. code = details.code;
  255. url = `chrome-extension://${extensionId}/${String(Math.random()).substr(2, 8)}.js`;
  256. }
  257. return ipcMainUtils.invokeInWebContents(contents, false, 'CHROME_TABS_EXECUTE_SCRIPT', extensionId, url, code);
  258. });
  259. exports.getContentScripts = () => {
  260. return Object.values(contentScripts);
  261. };
  262. // Transfer the content scripts to renderer.
  263. const contentScripts = {};
  264. const injectContentScripts = function (manifest) {
  265. if (contentScripts[manifest.name] || !manifest.content_scripts) return;
  266. const readArrayOfFiles = function (relativePath) {
  267. return {
  268. url: `chrome-extension://${manifest.extensionId}/${relativePath}`,
  269. code: String(fs.readFileSync(path.join(manifest.srcDirectory, relativePath)))
  270. };
  271. };
  272. const contentScriptToEntry = function (script) {
  273. return {
  274. matches: script.matches,
  275. js: script.js ? script.js.map(readArrayOfFiles) : [],
  276. css: script.css ? script.css.map(readArrayOfFiles) : [],
  277. runAt: script.run_at || 'document_idle',
  278. allFrames: script.all_frames || false
  279. };
  280. };
  281. try {
  282. const entry = {
  283. extensionId: manifest.extensionId,
  284. contentScripts: manifest.content_scripts.map(contentScriptToEntry)
  285. };
  286. contentScripts[manifest.name] = entry;
  287. } catch (e) {
  288. console.error('Failed to read content scripts', e);
  289. }
  290. };
  291. const removeContentScripts = function (manifest) {
  292. if (!contentScripts[manifest.name]) return;
  293. delete contentScripts[manifest.name];
  294. };
  295. // Transfer the |manifest| to a format that can be recognized by the
  296. // |DevToolsAPI.addExtensions|.
  297. const manifestToExtensionInfo = function (manifest) {
  298. return {
  299. startPage: manifest.startPage,
  300. srcDirectory: manifest.srcDirectory,
  301. name: manifest.name,
  302. exposeExperimentalAPIs: true
  303. };
  304. };
  305. // Load the extensions for the window.
  306. const loadExtension = function (manifest) {
  307. startBackgroundPages(manifest);
  308. injectContentScripts(manifest);
  309. };
  310. const loadDevToolsExtensions = function (win, manifests) {
  311. if (!win.devToolsWebContents) return;
  312. manifests.forEach(loadExtension);
  313. const extensionInfoArray = manifests.map(manifestToExtensionInfo);
  314. extensionInfoArray.forEach((extension) => {
  315. win.devToolsWebContents._grantOriginAccess(extension.startPage);
  316. });
  317. extensionInfoArray.forEach((extensionInfo) => {
  318. win.devToolsWebContents.executeJavaScript(`Extensions.extensionServer._addExtension(${JSON.stringify(extensionInfo)})`);
  319. });
  320. };
  321. app.on('web-contents-created', function (event, webContents) {
  322. if (!isWindowOrWebView(webContents)) return;
  323. hookWebContentsEvents(webContents);
  324. webContents.on('devtools-opened', function () {
  325. loadDevToolsExtensions(webContents, Object.values(manifestMap));
  326. });
  327. });
  328. // The chrome-extension: can map a extension URL request to real file path.
  329. const chromeExtensionHandler = function (request, callback) {
  330. const parsed = url.parse(request.url);
  331. if (!parsed.hostname || !parsed.path) return callback();
  332. const manifest = manifestMap[parsed.hostname];
  333. if (!manifest) return callback();
  334. const page = backgroundPages[parsed.hostname];
  335. if (page && parsed.path === `/${page.name}`) {
  336. // Disabled due to false positive in StandardJS
  337. // eslint-disable-next-line standard/no-callback-literal
  338. return callback({
  339. mimeType: 'text/html',
  340. data: page.html
  341. });
  342. }
  343. fs.readFile(path.join(manifest.srcDirectory, parsed.path), function (err, content) {
  344. if (err) {
  345. // Disabled due to false positive in StandardJS
  346. // eslint-disable-next-line standard/no-callback-literal
  347. return callback(-6); // FILE_NOT_FOUND
  348. } else {
  349. return callback(content);
  350. }
  351. });
  352. };
  353. app.on('session-created', function (ses) {
  354. ses.protocol.registerBufferProtocol('chrome-extension', chromeExtensionHandler);
  355. });
  356. // The persistent path of "DevTools Extensions" preference file.
  357. let loadedDevToolsExtensionsPath = null;
  358. app.on('will-quit', function () {
  359. try {
  360. const loadedDevToolsExtensions = Array.from(devToolsExtensionNames)
  361. .map(name => manifestNameMap[name].srcDirectory);
  362. if (loadedDevToolsExtensions.length > 0) {
  363. try {
  364. fs.mkdirSync(path.dirname(loadedDevToolsExtensionsPath));
  365. } catch {
  366. // Ignore error
  367. }
  368. fs.writeFileSync(loadedDevToolsExtensionsPath, JSON.stringify(loadedDevToolsExtensions));
  369. } else {
  370. fs.unlinkSync(loadedDevToolsExtensionsPath);
  371. }
  372. } catch {
  373. // Ignore error
  374. }
  375. });
  376. // We can not use protocol or BrowserWindow until app is ready.
  377. app.once('ready', function () {
  378. // The public API to add/remove extensions.
  379. BrowserWindow.addExtension = function (srcDirectory) {
  380. const manifest = getManifestFromPath(srcDirectory);
  381. if (manifest) {
  382. loadExtension(manifest);
  383. for (const webContents of getAllWebContents()) {
  384. if (isWindowOrWebView(webContents)) {
  385. loadDevToolsExtensions(webContents, [manifest]);
  386. }
  387. }
  388. return manifest.name;
  389. }
  390. };
  391. BrowserWindow.removeExtension = function (name) {
  392. const manifest = manifestNameMap[name];
  393. if (!manifest) return;
  394. removeBackgroundPages(manifest);
  395. removeContentScripts(manifest);
  396. delete manifestMap[manifest.extensionId];
  397. delete manifestNameMap[name];
  398. };
  399. BrowserWindow.getExtensions = function () {
  400. const extensions = {};
  401. Object.keys(manifestNameMap).forEach(function (name) {
  402. const manifest = manifestNameMap[name];
  403. extensions[name] = { name: manifest.name, version: manifest.version };
  404. });
  405. return extensions;
  406. };
  407. BrowserWindow.addDevToolsExtension = function (srcDirectory) {
  408. const manifestName = BrowserWindow.addExtension(srcDirectory);
  409. if (manifestName) {
  410. devToolsExtensionNames.add(manifestName);
  411. }
  412. return manifestName;
  413. };
  414. BrowserWindow.removeDevToolsExtension = function (name) {
  415. BrowserWindow.removeExtension(name);
  416. devToolsExtensionNames.delete(name);
  417. };
  418. BrowserWindow.getDevToolsExtensions = function () {
  419. const extensions = BrowserWindow.getExtensions();
  420. const devExtensions = {};
  421. Array.from(devToolsExtensionNames).forEach(function (name) {
  422. if (!extensions[name]) return;
  423. devExtensions[name] = extensions[name];
  424. });
  425. return devExtensions;
  426. };
  427. // Load persisted extensions.
  428. loadedDevToolsExtensionsPath = path.join(app.getPath('userData'), 'DevTools Extensions');
  429. try {
  430. const loadedDevToolsExtensions = JSON.parse(fs.readFileSync(loadedDevToolsExtensionsPath));
  431. if (Array.isArray(loadedDevToolsExtensions)) {
  432. for (const srcDirectory of loadedDevToolsExtensions) {
  433. // Start background pages and set content scripts.
  434. BrowserWindow.addDevToolsExtension(srcDirectory);
  435. }
  436. }
  437. } catch (error) {
  438. if (process.env.ELECTRON_ENABLE_LOGGING && error.code !== 'ENOENT') {
  439. console.error('Failed to load browser extensions from directory:', loadedDevToolsExtensionsPath);
  440. console.error(error);
  441. }
  442. }
  443. });