潘钜森 发表于 2023-9-18 16:27:27

【MutationObserver实例】监听样式变化还原样式

本帖最后由 潘钜森 于 2023-9-18 16:30 编辑

### \# 如下图,微软必应(关键词:苹果)搜索结果,品牌广告占屏🤬

!(data/attachment/forum/202309/18/155323asmv8vj2zjeqzzuu.png)

---

### \#\# 首先,把他的广告背景图给给干掉,杀了😇

```javascript
document.querySelector("#b_ims_bza_pole").style.setProperty("display", "none", "important");
```

!(data/attachment/forum/202309/18/155217l3znlu7x7yly3yzq.png)

---

### \#\# 正片开始🔞

!(data/attachment/forum/202309/18/161452tffvgvf1u0f0xugf.png)

```javascript
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
var bing_header = document.querySelector("#b_header");
var observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      if (mutation.type == "attributes") {
            bing_header.style.backgroundColor = "";
            bing_header.style.borderBottom = "1px solid #ececec";
      }
    });
});

observer.observe(bing_header, {
    attributes: true,
    attributeFilter: ['style']
});
```

!(data/attachment/forum/202309/18/161652kw4w263n79363g39.png)

---

### \#\#\# 🔥脚本实例如下:

```javascript
// ==UserScript==
// @name         【MutationObserver实例】监听样式变化还原样式
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      0.1.0
// @descriptiontry to take over the world!
// @author       You
// @match      *://*.bing.com/*
// @grant      GM_addStyle
// @run-at       document-start
// ==/UserScript==

GM_addStyle(`
#b_ims_bza_pole {
    display: none !important;
}
`);

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
var bing_header = document.querySelector("#b_header");
var observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      if (mutation.type == "attributes") {
            bing_header.style.backgroundColor = "";
            bing_header.style.borderBottom = "1px solid #ececec";
      }
    });
});

observer.observe(bing_header, {
    attributes: true,
    attributeFilter: ['style']
});
```

王一之 发表于 2023-9-18 17:00:10

这。。。刚刚试了一下,还有这种操作,广告屏蔽插件都没屏蔽掉(也可能因为刚出,还没支持)
页: [1]
查看完整版本: 【MutationObserver实例】监听样式变化还原样式