【问题标题】:How to make a static HTML as a chrome extension?如何将静态 HTML 制作为 chrome 扩展?
【发布时间】:2015-12-08 11:51:01
【问题描述】:

我想制作一个 Chrome 扩展,基本上构成一个 HTML 栏,一直停留在用户窗口的顶部(在他激活它之后)。酒吧的位置会是“喜欢”它:

我的问题是:我怎样才能实现这种行为?我考虑在清单中将其添加为“浏览器操作”,但它不适合,因为它在失去焦点时会消失。我还尝试将它添加为content script,用简单的 JS 命令document.write(HTML line)“拥抱”我的 HTML,但是当我尝试它时,我根本看不到任何栏。我应该如何进行?

【问题讨论】:

    标签: javascript html google-chrome google-chrome-extension content-script


    【解决方案1】:

    我认为 content_scripts 是去这里的方式。因此,您实际上修改了页面的 DOM 以显示您的内容。您可以查看其他类似的扩展。

    例如:StyleBot。他们是这样inject code to the page

    "content_scripts": [{
    ...
      "matches": ["<all_urls>"],
      ...
      "js": [
        ...
        "shared/modalbox/modalbox.js",
    

    这里是actual modal box的代码。

    this.box = $('<div>', {
        id: 'stylebot-modal'
    }).append(html);
    
    if (this.options.parent) {
        this.box.appendTo(this.options.parent);
    }
    else {
        this.box.appendTo(document.body);
    }
    
    this.box.css({
        height: this.options.height + '!important',
        width: this.options.width + ' !important',
        top: this.options.top + ' !important',
        left: this.options.left + ' !important'
    });
    

    【讨论】:

    • 谢谢" 但是你能解释一下它如何适合我的情况吗?我不能对我创建的元素使用“appendTo”...
    • 为什么不能使用 appendTo?
    • 好的,现在我看到我可以使用 JQuery 创建元素了。但是,无论如何,它几乎可以工作,但它并没有出现在每个站点中(实际上它只是在 Google 中工作......)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 2020-08-21
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    相关资源
    最近更新 更多