Chrome应用程序/扩展程序要删除页面元素吗? [英] Chrome App/extension to remove a page element?
问题描述
我需要创建一个Chrome扩展程序来处理某些页面上的HTML.
I need to create a Chrome Extension to manipulate the HTML on some pages.
基本上,我需要做的是删除页面内的< div class ="feedmain">
.我目前可以使用开发人员工具删除该< div>
,但是当我重新加载页面时,显然可以重新加载该元素,因此我想开发一些扩展程序来自动为我执行此操作
Basically, what I need to do is to remove a <div class="feedmain">
inside a page. I'm currently able to remove that <div>
using the developer tools, but the element is obviously reloaded when I reload the page, so I would like to develop some extension that does this for me automatically.
所以我想知道是否有可能,以及如何在扩展中完成的概述.
So I would like to know if it is possible, and an overview of how it can be done in an extension.
推荐答案
是的,绝对可以,而且很容易做到:您所需要的只是一个内容脚本,该脚本将注入到该特定页面中,以为您删除元素.
Yes, absolutely, that's possible and pretty easy to do: all you need is a content script that will be injected into that particular page to remove the element for you.
要做您想做的事
- 创建一个简单的空Chrome扩展程序(请参见 此处 官方指南)和
manifest.json
文件. -
在
manifest.json
中指定"content_scripts"
字段,然后声明要注入的脚本.像这样:
- Create a simple empty Chrome Extension (see here for the official guide) with a
manifest.json
file. Specify the
"content_scripts"
field in themanifest.json
, and declare the script you want to inject. Something like this:
"content_scripts": [
{
"matches": ["http://somesite.com/somepage.html"],
"js": ["/content_script.js"]
}
]
> 此处 ,以了解内容脚本的工作原理.
Take a look here to find out how content scripts work.
创建一个 content_script.js
,它将为您删除该元素.该脚本基本上只包含以下两行:
Create a content_script.js
, which will delete the element for you. This script will basically only contain the following two lines:
var element = document.querySelector('div.feedmain');
element.parentElement.removeChild(element);
这篇关于Chrome应用程序/扩展程序要删除页面元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!