From 0b2481cdc70fd44c4679e8f1fb44cb45e44bf885 Mon Sep 17 00:00:00 2001 From: "yh106.jung" Date: Wed, 18 Oct 2017 17:27:13 +0900 Subject: [PATCH] [PDNCF] Add support for css in content_scripts Add support for css in content_scripts Change-Id: Icd00d1f94c34cb6b7dca4be0d774537df8ad819f Signed-off-by: yh106.jung --- lib/browser/chrome-extension.js | 3 ++- lib/renderer/content-scripts-injector.js | 28 ++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/browser/chrome-extension.js b/lib/browser/chrome-extension.js index d90a360..0f1c7d4 100644 --- a/lib/browser/chrome-extension.js +++ b/lib/browser/chrome-extension.js @@ -239,7 +239,8 @@ const injectContentScripts = function (manifest) { const contentScriptToEntry = function (script) { return { matches: script.matches, - js: script.js.map(readArrayOfFiles), + js: script.js ? script.js.map(readArrayOfFiles) : [], + css: script.css ? script.css.map(readArrayOfFiles) : [], runAt: script.run_at || 'document_idle' } } diff --git a/lib/renderer/content-scripts-injector.js b/lib/renderer/content-scripts-injector.js index e4a8011..124fd58 100644 --- a/lib/renderer/content-scripts-injector.js +++ b/lib/renderer/content-scripts-injector.js @@ -30,14 +30,26 @@ const injectContentScript = function (extensionId, script) { if (!matchesPattern(match)) return } - for (const {url, code} of script.js) { - const fire = runContentScript.bind(window, extensionId, url, code) - if (script.runAt === 'document_start') { - process.once('document-start', fire) - } else if (script.runAt === 'document_end') { - process.once('document-end', fire) - } else if (script.runAt === 'document_idle') { - document.addEventListener('DOMContentLoaded', fire) + if (script.js) { + for (const {url, code} of script.js) { + const fire = runContentScript.bind(window, extensionId, url, code) + if (script.runAt === 'document_start') { + process.once('document-start', fire) + } else if (script.runAt === 'document_end') { + process.once('document-end', fire) + } else if (script.runAt === 'document_idle') { + document.addEventListener('DOMContentLoaded', fire) + } + } + } + + if (script.css) { + for (const {code} of script.css) { + process.once('document-end', () => { + var node = document.createElement('style') + node.innerHTML = code + window.document.body.appendChild(node) + }) } } } -- 2.7.4