css - 如何将 CSS 正确加载到 Chrome 扩展 popup.html 中?

标签 css google-chrome-extension

内容:我正在创建一个 Chrome 扩展程序。

设置: 当我单击扩展图标时,它会将 popup.html 作为窗口加载。我正在尝试使用此代码加载 JSON 数据表 http://bootstrap-table.wenzhixin.net.cn/examples/变成漂亮的 HTML 表格。

问题:表格加载正常。 javascript 似乎工作正常,但样式表似乎不工作。我链接到 popup.html 头部的本地样式表,当我像这样在 Chrome 中单击扩展程序图标时加载...

<link rel="stylesheet" type="text/css" href="bootstrap-table.css">

问题:我需要将它添加到 list 中的某处吗?我只需要弹出 html 的样式表。我不需要将它注入(inject)网页。我只是想显示一个漂亮的 html 表格。

list .json

{
  "manifest_version": 2,

  "name": "Chrome Extension",
  "description": "Analyze page.",
  "version": "0.1",
  "icons": { "32": "icon32.png",
           "72": "icon72.png",
          "114": "icon114.png",
          "144": "icon144.png" },

  "browser_action": {
    "default_icon": "icon32.png",
    "default_popup": "popup.html"
  },
 "web_accessible_resources": [
    "bootstrap-table.css",
  ],
  "permissions": [
    "activeTab",
  ]
}

// see http://bootstrap-table.wenzhixin.net.cn/documentation/
// see http://issues.wenzhixin.net.cn/bootstrap-table/#methods/getSelections.html
var data = [
    {
        "name": "bootstrap-table",
        "stargazers_count": "526",
        "forks_count": "122",
        "description": "An extended Bootstrap table with radio, checkbox, sort, pagination, and other added features. (supports twitter bootstrap v2 and v3) "
    },
    {
        "name": "multiple-select",
        "stargazers_count": "288",
        "forks_count": "150",
        "description": "A jQuery plugin to select multiple elements with checkboxes :)"
    },
    {
        "name": "bootstrap-show-password",
        "stargazers_count": "32",
        "forks_count": "11",
        "description": "Show/hide password plugin for twitter bootstrap."
    },
    {
        "name": "blog",
        "stargazers_count": "13",
        "forks_count": "4",
        "description": "my blog"
    },
    {
        "name": "scutech-redmine",
        "stargazers_count": "6",
        "forks_count": "3",
        "description": "Redmine notification tools for chrome extension."
    }
];


function renderStatus(statusText) {
  document.getElementById('status').textContent = statusText;
}

// MAIN CODE: on click of extension icon
document.addEventListener('DOMContentLoaded', function() {

    //renderStatus('Test1');
    //$('#status').append('Test2');

    $(function () {
        $('#table').bootstrapTable({
            data: data
        });

        var $table = $('#table');
        $('#select-button').click(function () {
            var msg = 'getSelections: ' + JSON.stringify($table.bootstrapTable('getSelections'));
            renderStatus(msg);
        });
    });
  
});
<!doctype html>
<html>
  <head>
    <title>Chrome Extension</title>

    <link rel="stylesheet" type="text/css" href="bootstrap-table.css">
    <style>
    body{
width:820px;
height:400px;
}

#table{
width:100%;
}
    </style>
    <script type="text/javascript" src="jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="bootstrap-table.js"></script>
    <script type="text/javascript" src="popup.js"></script>

  </head>
  <body>

  <div id="status"></div>

  <div class="toolbar">
    <button id="select-button" class="btn btn-default">Selected Rows</button>
    <button type="button" class="btn btn-default">
        <i class="glyphicon glyphicon-plus"></i>
    </button>
    <button type="button" class="btn btn-default">
        <i class="glyphicon glyphicon-heart"></i>
    </button>
    <button type="button" class="btn btn-default">
        <i class="glyphicon glyphicon-trash"></i>
    </button>
  </div>

  <table
    data-show-columns="true"
    data-toolbar="#toolbar"
    data-search="true"
       data-show-refresh="true"
    data-height="460"
    id="table">
    <thead>
    <tr>
        <th data-field="state" data-checkbox="true"></th>
        <th data-field="name"
            data-switchable="false"
            data-sortable="true">
                Name
        </th>
        <th data-field="stargazers_count"
            data-sortable="true">
                Stars
        </th>
        <th data-field="forks_count"
            data-sortable="true">
                Forks
        </th>
        <th data-field="description"
            data-visible="false"
            data-sortable="true">
                Description
        </th>
    </tr>
    </thead>
    </table>

  </body>
</html>

最佳答案

根据我的经验,从弹出窗口中引用扩展中包含的 CSS 文件确实可以工作,而无需添加任何特定于 list 的 CSS。

修改 list 使其加载后,您上面的示例对我有用,生成格式正确的表格。我使用的 list :

{
  "manifest_version": 2,

  "name": "Chrome Extension",
  "description": "Analyze page.",
  "version": "0.1",
  "browser_action": {
      "default_popup": "popup.html"
  },
  "permissions": [
    "activeTab"
  ]
}

关于css - 如何将 CSS 正确加载到 Chrome 扩展 popup.html 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41494107/

相关文章:

html - 表格的奇怪对齐问题

javascript - Jquery child>Parent>child>child 选择器

javascript - 来自 background.js 的内容脚本中的 Chrome 调用函数

google-chrome-extension - Chrome 扩展中的 JavaScript 注入(inject)

google-chrome-extension - 重启浏览器后右键菜单消失

css - 在画廊图像缩略图 CSS 上悬停时显示标题

html - 为什么我的 body 不接受我的利润率?

css - polymer 和共享样式。在整个元素中使用 font-awesome 这样的字体

javascript - Chrome 扩展突出显示文本选择 chrome.tabs

javascript - 如何在通过 GET 请求发送之前使用 JavaScript 获取 Gmail 附件