javascript - How do I edit live css using tampermonkey without using Amino: Live CSS Editor - Stack Overflow

I wondering if it's possible to create a Tampermonkey script with javascript that will find and re

I wondering if it's possible to create a Tampermonkey script with javascript that will find and replace an existing html or css element that already exists.

In my case I want to add this to the css element at the domain google to change display to none:

    #tvcap {
        display: none;
    }

This prevents me from seeing annoying ads. I use Pi-hole, so at this point they are just taking up space on my screen.

I installed Amino: Live CSS Editor chrome extension and it's working great. I only have to use these 3 lines of code but I'd preferably want to use Tampermonkey with a JS script.

example:

Before:

After:

I wondering if it's possible to create a Tampermonkey script with javascript that will find and replace an existing html or css element that already exists.

In my case I want to add this to the css element at the domain google. to change display to none:

    #tvcap {
        display: none;
    }

This prevents me from seeing annoying ads. I use Pi-hole, so at this point they are just taking up space on my screen.

I installed Amino: Live CSS Editor chrome extension and it's working great. I only have to use these 3 lines of code but I'd preferably want to use Tampermonkey with a JS script.

example:

Before:

After:

Share Improve this question edited Dec 13, 2020 at 7:35 coder9741 asked Dec 13, 2020 at 7:12 coder9741coder9741 1011 silver badge12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You could use a simple utility function like this:

function addStyle(styleText){
    let s = document.createElement('style')
    s.appendChild(document.createTextNode(styleText))
    document.getElementsByTagName('head')[0].appendChild(s)
}

Using the template string format, it is now easy to add css:

addStyle(`
  body{
    color:red;
  }
`)

That's really a nice idea.

You can't directly change the CSS in the way you are doing.

You need to find the element using the ID with getElementById and change the style property of the HTML element

In your case, simplest code will be:

document.getElementById("tvcap").style.display = "none";

Daniele

This works.

// ==UserScript==
// @name         Block google ads
// @namespace    http://tampermonkey/
// @version      0.1
// @description  try to take over the world!
// @author       You, credit: https://stackoverflow./users/5053475/daniele-rugginenti
// @match        https://*/*
// @grant        none
// ==/UserScript==



(function() {
    'use strict';

    document.getElementById("tvcap").style.display = "none";
})();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744733070a4590579.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信