Skip to content

Instantly share code, notes, and snippets.

@Inviz
Created June 3, 2014 19:03
Show Gist options
  • Save Inviz/f077135c2855fa019f97 to your computer and use it in GitHub Desktop.
Save Inviz/f077135c2855fa019f97 to your computer and use it in GitHub Desktop.
MutationObserver shim: setAttribute/removeAttribute redefinition for Phantom.js
var attrModifiedWorks = false;
var listener = function(){ attrModifiedWorks = true; };
document.documentElement.addEventListener("DOMAttrModified", listener, false);
document.documentElement.setAttribute("___TEST___", true);
document.documentElement.removeAttribute("___TEST___", true);
document.documentElement.removeEventListener("DOMAttrModified", listener, false);
HTMLElement.prototype.__removeAttribute = HTMLElement.prototype.removeAttribute;
HTMLElement.prototype.removeAttribute = function(attrName)
{
console.error("REMOVE ATTT")
var prevVal = this.getAttribute(attrName);
this.__removeAttribute(attrName);
var evt = document.createEvent("MutationEvent");
evt.initMutationEvent(
"DOMAttrModified",
true,
false,
this,
prevVal,
"",
attrName,
evt.REMOVAL
);
this.dispatchEvent(evt);
}
HTMLElement.prototype.__setAttribute = HTMLElement.prototype.setAttribute
HTMLElement.prototype.setAttribute = function(attrName, newVal)
{
var prevVal = this.getAttribute(attrName);
this.__setAttribute(attrName, newVal);
newVal = this.getAttribute(attrName);
if (window.zzz)
console.error("SET ATTT", newVal)
if (newVal != prevVal)
{
var evt = document.createEvent("MutationEvent");
evt.initMutationEvent(
"DOMAttrModified",
true,
false,
this,
prevVal || "",
newVal || "",
attrName,
(prevVal == null) ? evt.ADDITION : evt.MODIFICATION
);
evt.prevValue = prevVal
evt.attrName = attrName
this.dispatchEvent(evt);
}
}
// Doesnt fire updates in PhantomJS, IE<=8
el.style.width = '1100px'
// Does fire updates, but resets styles set via .style. Should probably restore them
el.setAttribute('style', 'width: 1100px')cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment