简体   繁体   English

从chrome扩展的后台页面注入HTML

[英]Injecting HTML from background page of a chrome extention

I am working on a chrome extension and would like to inject some HTML code to the DOM in a specific situation (it will result in a message being shown to the user). 我正在研究chrome扩展,并希望在特定情况下向DOM注入一些HTML代码(这将导致向用户显示消息)。 However - i would like to do this from the background page and not from the content script. 但是 - 我想从后台页面而不是内容脚本中执行此操作。 Is this possible? 这可能吗? can the background page manipulate the DOM? 后台页面可以操纵DOM吗?

Thanks! 谢谢!

You can't do this directly. 你不能直接这样做。 Instead send a message from your background page to a content script with all required data for the script to create proper HTML code or inject the code using chrome.tabs.executeScript . 而是从后台页面向内容脚本发送消息,其中包含脚本创建正确HTML代码或使用chrome.tabs.executeScript注入代码所需的所有数据。

You can use something like this script in your background.js 您可以在background.js中使用类似此脚本的内容

chrome.tabs.executeScript({
    code: "$('body').prepend('Some html')"
});

or 要么

chrome.tabs.executeScript({
    file: "insert.js"
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM