javascript - 如何在 webview.addContentScripts 之后在正文中插入 css 文件

标签 javascript css google-chrome google-chrome-app

我使用以下代码将 css 文件添加到 webview

  webview.addContentScripts([
    {
      name: 'myRule',
      matches: ['https://app.yinxiang.com/*'],
      css: {files: ["vendor/bootstrap.css"]},
      js: {files: ["vendor/jq.js", "vendor/keymaster.js", "ctn.js", "find_dialog.js"]},
      run_at: 'document_start'
    }]
  )

在 find_dialog.js 中

$(document).ready(function () {`
  $("body").append(`
  <link rel="stylesheet" href="vendor/bootstrap.css">
`)
 ...

在 webview devtool 中,它引发

Failed to load resource: the server responded with a status of 404 (Not Found) -- https://app.yinxiang.com/vendor/bootstrap.css 

如何添加vendor/bootstrap.css?

最佳答案

似乎您正在尝试将 Bootstrap CSS 的本地副本添加到页面中。

但是,当您通过添加显式 <link> 来实现时元素,并提供一个相对路径,它是相对于相关页面(在本例中为 https://app.yinxiang.com/ ),而不是您的应用程序。

如果需要,您可以从内容脚本中引用内部应用内容,但您必须做两件事:

也就是说,我认为您根本不需要这样做;您已经将 CSS 声明为与您的内容脚本一起注入(inject)。它应该已经包含在您的 find_dialog.js 中了,所以你的额外代码是多余的。如果不是,请仔细检查您的(本地、相对)路径。

关于javascript - 如何在 webview.addContentScripts 之后在正文中插入 css 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39824815/

相关文章:

javascript - 导航不适用于 ngif 条件

javascript - Angular 2 - 如何使用其他组件作为服务并获取其数据

html - CSS 在 jQuery 拖放后没有正确更新,但在 Inspector 中可以

javascript - liferay设置liferay-ui的id为:search-container-row

javascript - 动态创建的 html 元素呈现为文本

html - CSS : creating cornered border using css

javascript - jquery 替换 HTML div 值

html - 跨越列表到多行

javascript - 如何更改 javascript 中的宽度属性?

javascript - Revolution slider 动画在 chrome 中不起作用,但在 firefox 中不起作用