content-scripts-injector.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const {ipcRenderer} = require('electron')
  2. const {runInThisContext} = require('vm')
  3. // Check whether pattern matches.
  4. // https://developer.chrome.com/extensions/match_patterns
  5. const matchesPattern = function (pattern) {
  6. if (pattern === '<all_urls>') return true
  7. const regexp = new RegExp(`^${pattern.replace(/\*/g, '.*')}$`)
  8. const url = `${location.protocol}//${location.host}${location.pathname}`
  9. return url.match(regexp)
  10. }
  11. // Run the code with chrome API integrated.
  12. const runContentScript = function (extensionId, url, code) {
  13. const context = {}
  14. require('./chrome-api').injectTo(extensionId, false, context)
  15. const wrapper = `((chrome) => {\n ${code}\n })`
  16. const compiledWrapper = runInThisContext(wrapper, {
  17. filename: url,
  18. lineOffset: 1,
  19. displayErrors: true
  20. })
  21. return compiledWrapper.call(this, context.chrome)
  22. }
  23. const runAllContentScript = function (scripts, extensionId) {
  24. for (const {url, code} of scripts) {
  25. runContentScript.call(window, extensionId, url, code)
  26. }
  27. }
  28. const runStylesheet = function (url, code) {
  29. const wrapper = `((code) => {
  30. function init() {
  31. const styleElement = document.createElement('style');
  32. styleElement.textContent = code;
  33. document.head.append(styleElement);
  34. }
  35. document.addEventListener('DOMContentLoaded', init);
  36. })`
  37. const compiledWrapper = runInThisContext(wrapper, {
  38. filename: url,
  39. lineOffset: 1,
  40. displayErrors: true
  41. })
  42. return compiledWrapper.call(this, code)
  43. }
  44. const runAllStylesheet = function (css) {
  45. for (const {url, code} of css) {
  46. runStylesheet.call(window, url, code)
  47. }
  48. }
  49. // Run injected scripts.
  50. // https://developer.chrome.com/extensions/content_scripts
  51. const injectContentScript = function (extensionId, script) {
  52. if (!script.matches.some(matchesPattern)) return
  53. if (script.js) {
  54. const fire = runAllContentScript.bind(window, script.js, extensionId)
  55. if (script.runAt === 'document_start') {
  56. process.once('document-start', fire)
  57. } else if (script.runAt === 'document_end') {
  58. process.once('document-end', fire)
  59. } else {
  60. document.addEventListener('DOMContentLoaded', fire)
  61. }
  62. }
  63. if (script.css) {
  64. const fire = runAllStylesheet.bind(window, script.css)
  65. if (script.runAt === 'document_start') {
  66. process.once('document-start', fire)
  67. } else if (script.runAt === 'document_end') {
  68. process.once('document-end', fire)
  69. } else {
  70. document.addEventListener('DOMContentLoaded', fire)
  71. }
  72. }
  73. }
  74. // Handle the request of chrome.tabs.executeJavaScript.
  75. ipcRenderer.on('CHROME_TABS_EXECUTESCRIPT', function (event, senderWebContentsId, requestId, extensionId, url, code) {
  76. const result = runContentScript.call(window, extensionId, url, code)
  77. ipcRenderer.sendToAll(senderWebContentsId, `CHROME_TABS_EXECUTESCRIPT_RESULT_${requestId}`, result)
  78. })
  79. // Read the renderer process preferences.
  80. const preferences = process.getRenderProcessPreferences()
  81. if (preferences) {
  82. for (const pref of preferences) {
  83. if (pref.contentScripts) {
  84. for (const script of pref.contentScripts) {
  85. injectContentScript(pref.extensionId, script)
  86. }
  87. }
  88. }
  89. }