javascript - How to remove a UI element from a web page that's not present at document.ready? -


i'm regular user of site https://www.lingq.com. site added "share" side panel, slides in left few seconds after page loaded. it's annoying, covers portion of text on site. after while got fed having manually click away every time. i'm using greasemonkey remove it, problem element not yet there when document.ready occurs.

i'm getting around using timeouts.

jquery(document).ready(function () {   settimeout(function () {     jquery('#at4-share').remove()   }, 5000)   settimeout(function () {     jquery('#at4-share').remove()   }, 10000) }); 

after experimenting i've settled on 2 timers. first 1 (after 5 seconds) removes thing moment shows up. second 1 there because sidebar not have appeared in time. site quite bit of javascript processing upon loading, , thing appears after it's done.

while solution works, it's crude , ugly. i'd solution reliably removes panel, no matter how long takes appear. ideally, shouldn't see panel on page. suggestions?

they implemented in firefox mutationobserver , in chrome vendor-prefixed webkitmutationobserver:

mutationobserver = window.mutationobserver || window.webkitmutationobserver;  var observer = new mutationobserver(function(mutations, observer) {     // fired when mutation occurs     console.log(mutations, observer);     // ... });  // define element should observed observer // , types of mutations trigger callback observer.observe(document, {   subtree: true,   attributes: true   //... }); 

this example listens dom changes on document , entire subtree, , fire on changes element attributes structural changes. draft spec has full list of valid mutation listener properties:

childlist

set true if mutations target's children observed.

attributes

set true if mutations target's attributes observed.

characterdata

set true if mutations target's data observed.

subtree

set true if mutations not target, target's descendants observed.

attributeoldvalue

set true if attributes set true , target's attribute value before mutation needs recorded.

characterdataoldvalue

set true if characterdata set true , target's data before mutation needs recorded.

attributefilter

set list of attribute local names (without namespace) if not attribute mutations need observed.

this should work you:

mutationobserver = window.mutationobserver || window.webkitmutationobserver;  var observer = new mutationobserver(function(mutations, observer) {     jquery('#at4-share').remove(); });  observer.observe(document, {   subtree: true }); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -