简体   繁体   English

Chrome 扩展程序:如何将 CSS 和 Javascript 注入某个 URL?

[英]Chrome Extension: How do I inject CSS and Javascript into a certain URL?

I want to make a chrome extension that changes the website of ROBLOX back to the 2016 UI and other stuff.我想制作一个 chrome 扩展,将 ROBLOX 的网站改回 2016 年的 UI 和其他东西。

I currently have this UI in the popup: click here我目前在弹出窗口中有此 UI:单击此处

I already have some code to make the checkboxes work, but how do I actually make them add some CSS and Javascript to https://roblox.com ?我已经有一些代码可以让复选框工作,但我如何真正让它们向https://roblox.com添加一些 CSS 和 Javascript?

Edit : I already checked How to inject CSS into webpage through Chrome extension?编辑:我已经检查了如何通过 Chrome 扩展程序将 CSS 注入网页? but I want to do it programmatically.但我想以编程方式进行。

Updates:更新:

Sunday, November 22, 2020 17:53 Googled more, still didn't find anything Monday, November 23, 2020 17:40 Googled more, still didn't find anything. 2020 年 11 月 22 日,星期日 17:53谷歌搜索更多,仍然没有找到任何东西,2020 年 11 月 23 日星期一 17:40谷歌搜索更多,仍然没有找到任何东西。 Please answer!请回答! Nothing helps or works!没有任何帮助或工作!

Should get you started: you need to inject it into the tab you have the site open on, look at the example here on injecting CSS: Chrome extension: insertCSS once per tab应该让你开始:你需要将它注入到你打开网站的选项卡中,查看这里关于注入 CSS 的示例: Chrome 扩展:每个选项卡一次插入 CSS

It won't be as simple as that and you'll need to add logic to replace the css via the content script.它不会那么简单,您需要添加逻辑以通过内容脚本替换 css。

To inject a content script:注入内容脚本:

import browser from "webextension-polyfill";
    
await browser.tabs.executeScript(tabId, {
     file: "/static/js/content.js",
});

Add /static/js/content.js and any files you want to inject to your manifest添加/static/js/content.js和任何你想注入到清单中的文件

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

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