简体   繁体   English

将按钮注入网站(Chrome扩展程序内容脚本)

[英]Inject button into site (Chrome Extension content script)

I'm trying to inject a button onto a page using Chrome's content scripts, but the button never appears, and I get no error in the console. 我正在尝试使用Chrome的内容脚本在页面上注入一个按钮,但按钮从未出现,我在控制台中没有出现任何错误。

My manifest.json file: 我的manifest.json文件:

{
  "name": "Test",
  "version": "0.0.1",
  "manifest_version": 2,
  "description": "Test",
  "default_locale": "en",
  "permissions": [
    "<all_urls>"
  ],
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "src/inject/inject.js"
      ]
    }
  ]
}

and my inject.js file: 和我的inject.js文件:

document.addEventListener('DOMContentLoaded', function () {
    var buttonOnSite = document.getElementById("buttonOnSite");
    var button = document.createElement("button");
    var text = document.createTextNode("test");
    button.appendChild(text);
    buttonOnSite.appendChild(button);
});

What I expect from the above code, is when I go to the site where a button with the id buttonOnSite exists, a new button, with the text test is created after it. 我对上述代码的期望是,当我访问一个id为buttonOnSite的按钮存在的网站时,会出现一个新按钮,并在其后创建文本test

But nothing happens when I go to this site with the button! 但是当我带着按钮去这个网站时没有任何反应! There is definitely a button with the id buttonOnSite (I've changed the ID for here, because it's a long ID). 肯定有一个带有id buttonOnSite的按钮(我在这里更改了ID,因为它是一个很长的ID)。

I get no error message in the console, so what's wrong? 我在控制台中没有收到任何错误消息,那有什么不对? It's probably something obvious, but I just can't see it. 这可能是显而易见的,但我看不到它。 Any help is appreciated! 任何帮助表示赞赏!

This is the code you need: 这是您需要的代码:

var buttonOnSite = document.getElementById("buttonOnSite"),
    parent = buttonOnSite.parentElement,
    next = buttonOnSite.nextSibling,
    button = document.createElement("button"),
    text = document.createTextNode("test");

button.appendChild(text);
if (next) parent.insertBefore(button, next);
else parent.appendChild(button);

By default, content scripts run after the DOM has loaded. 默认情况下,内容脚本在加载DOM后运行。 This means that you're adding your event listener after the event has fired, so your listener never hears it. 这意味着您在事件触发后添加事件侦听器,因此您的侦听器永远不会听到它。 Remove the first and last lines of your javascript. 删除javascript的第一行和最后一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Chrome扩展程序:创建标签然后将内容脚本注入其中 - Chrome extension: create tab then inject content script into it 我无法将脚本注入带有 content.js chrome 扩展的站点 - i can't inject script to a site with content.js chrome extension 以编程方式在扩展程序上注入内容脚本(Chrome扩展程序) - Inject content script on extension reload programmatically (chrome extension) 在chrome扩展中,如何使用内容脚本注入一个Vue页面 - In chrome extension, how to use content script to inject a Vue page 如何在 Chrome 扩展中使用内容脚本文件注入 CSS? - How to inject CSS using content script file in Chrome extension? Chrome扩展程序如何将我的内容脚本注入所有Facebook页面 - Chrome extension how to inject my content script into all Facebook pages 在 Chrome 扩展内容脚本中注入 Popper.js - Inject Popper.js in Chrome Extension Content script Chrome扩展程序内容脚本 - 在页面代码之前注入Javascript - Chrome Extension Content Script - Inject Javascript before page code 从带有 chrome 扩展 v3 的内容脚本中注入 javascript - Inject javascript from content script with a chrome extension v3 Chrome 扩展 - 为特定站点执行内容脚本 url - Chrome extension - Execute content script for specific site url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM