Merge pull request #10076 from alexstrat/add/add-support-for-content_scripts.css

Add support for css in content_scripts
This commit is contained in:
Cheng Zhao 2017-09-14 21:19:11 +09:00 committed by GitHub
commit fc99785314
2 changed files with 22 additions and 9 deletions

View file

@ -239,7 +239,8 @@ const injectContentScripts = function (manifest) {
const contentScriptToEntry = function (script) { const contentScriptToEntry = function (script) {
return { return {
matches: script.matches, 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' runAt: script.run_at || 'document_idle'
} }
} }

View file

@ -28,6 +28,7 @@ const runContentScript = function (extensionId, url, code) {
const injectContentScript = function (extensionId, script) { const injectContentScript = function (extensionId, script) {
if (!script.matches.some(matchesPattern)) return if (!script.matches.some(matchesPattern)) return
if (script.js) {
for (const {url, code} of script.js) { for (const {url, code} of script.js) {
const fire = runContentScript.bind(window, extensionId, url, code) const fire = runContentScript.bind(window, extensionId, url, code)
if (script.runAt === 'document_start') { if (script.runAt === 'document_start') {
@ -40,6 +41,17 @@ const injectContentScript = function (extensionId, script) {
} }
} }
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)
})
}
}
}
// Handle the request of chrome.tabs.executeJavaScript. // Handle the request of chrome.tabs.executeJavaScript.
ipcRenderer.on('CHROME_TABS_EXECUTESCRIPT', function (event, senderWebContentsId, requestId, extensionId, url, code) { ipcRenderer.on('CHROME_TABS_EXECUTESCRIPT', function (event, senderWebContentsId, requestId, extensionId, url, code) {
const result = runContentScript.call(window, extensionId, url, code) const result = runContentScript.call(window, extensionId, url, code)