首页 > 解决方案 > 我制作的用户脚本部分有效。有什么修复吗?

问题描述

我正在制作一个用户脚本来删除Kissanime主页上的广告

动漫网站已经为您提供了隐藏广告的按钮,但我只想删除该按钮,但它不起作用。这是我的代码

// ==UserScript==
// @name         Kissanime Tool
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Kissanime Tool
// @author       Joe Hill
// @match        *://kissanime.ru/
// @grant        none
// @require https://code.jquery.com/jquery-3.3.1.min.js
// ==/UserScript==

(function() {
    'use strict';

    // The code below is an example of the working code
    $("#keyword").attr("value", "The script wrote here");
    //The code below doesn't work
    $(".divCloseBut").remove();
})();

我不确定为什么这不起作用。

标签: javascriptjqueryuserscripts

解决方案


按钮是动态创建的,具有:

function AddHideButtonToDynamic() {
  // some conditions
  $(elemDyna).after('<div class="divCloseBut" style="....')
}
window.setTimeout(AddHideButtonToDynamic, 5000);

function AddCloseButton(id) {
  // some conditions
  elem.after('<div class="divCloseBut" style="z-index:1000; ...')
}

拦截这些函数调用是可能的(虽然有点烦人) - 但是,注入一个<style>提供按钮的标签会更容易display: none,这可以在页面加载时完成一次,而且只能完成一次,所以你不必等待元素出现:

document.body.appendChild(document.createElement('style')).textContent = `
.divCloseBut {
  display: none !important;
}`;

推荐阅读