2

I want to change some dom content such that all input fields value is "" of a web page before the user can see it .How can I do that? Is there any way to do that? Thanks.

  • @wOxxOm I only want to make input fields empty before user can see ? –  Sep 21 '16 at 13:27
  • I use this `$('input[type="text"]').val("");` in content script . When webpage load completely after sometime (milliseconds) then content script runs and makes input filed empty but i need it before load so that user can not see saved content . –  Sep 21 '16 at 13:41

1 Answers1

5

You need a content script that runs on document_start, and either of the two methods:

  1. MutationObserver (the docs) but it'll slow down page loading:
  2. Add a CSS style override to make INPUT text transparent, and when the page is loaded clear the elements and remove the style:

    manifest.json:

    "content_scripts": [{
        "matches": ["<all_urls>"],
        "run_at": "document_start",
        "all_frames": true,
        "js": ["content.js"]
    }],
    

    content.js:

    // document is empty now so we'll only add the style
    var style = document.documentElement.appendChild(document.createElement('style'));
    style.textContent = 'input {color: transparent !important;}';
    
    // once DOM is ready clear the inputs
    document.addEventListener('DOMContentLoaded', function() {
        var inputs = document.getElementsByTagName('input');
        for (var i = inputs.length; --i >= 0; ) {
            var input = inputs[i];
            if (input.value) {
                input.value = '';
            }
        }
        style.remove();
    });
    
Community
  • 1
  • 1
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • I wanted to hide some of the elements on my webpage and I was doing them by deleting the elements once they load. After looking at your answer I started using css to set the display none of those elements. – rbansal Sep 20 '21 at 05:15
  • @rbansal, if hiding is sufficient for you, declare a `css` content script instead of `js` in manifest.json. – wOxxOm Sep 20 '21 at 05:35
  • That makes sense. Thanks – rbansal Sep 20 '21 at 05:41
  • How can I nullify a css rule? Some website adds a class which changes the color of a heading. I want to write a css rule to nullify all the properties set by the class by my extension. Example col {color:pink;}, col class is present in some div of the website and I want to nullify all the properties set by the col class through my extension. – rbansal Sep 24 '21 at 17:33
  • If it was hiding the element .col{display: none !important} was sufficient, but is there a way like .col { nullify} which will make the future occurring of the col class do not have impact on the properties on the div. – rbansal Sep 24 '21 at 17:34
  • Please don't use comments for extended discussions. Ask a new question with an [MCVE](/help/mcve). – wOxxOm Sep 24 '21 at 17:43