Forked from gerardpaapu/dft-position-unfixed.js
Last active
October 10, 2015 19:28
-
-
Save lionicsheriff/3740005 to your computer and use it in GitHub Desktop.
unfix position
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @id dft-postion-unfixed | |
// @name Remove position fixed | |
// @version 2.0 | |
// @namespace dft | |
// @author Matthew Goodall | |
// @description Changes position:fixed to position:relative | |
// @include * | |
// @exclude https://docs.google.com/* | |
// @run-at document-end | |
// ==/UserScript== | |
// remove position fixed from an element | |
function unfix(element){ | |
if (window.getComputedStyle(element).position == 'fixed'){ | |
element.style.position = 'static'; | |
} | |
} | |
// remove position fixed from an elment and its children | |
function unfixRecursive(element){ | |
var i, child; | |
if(element.childNodes){ | |
for(i=0; i < element.childNodes.length; i++){ | |
child = element.childNodes[0]; | |
window.setTimeout(unfixRecursive,0,child); // allow the function to exit earlier | |
} | |
} | |
unfix(element); | |
} | |
// Set up an observer to remove position:fixed from elements when they are changed/added | |
var observer = MutationObserver(function(records){ | |
var i,j,records, element; | |
for (i = 0; i < records.length; i++){ | |
record = records[i]; | |
if(record.type == 'childList'){ | |
if (record.addedNodes){ | |
for(j = 0; j < record.addedNodes.length; j++){ | |
element = record.addedNodes[j]; | |
unfixRecursive(element); | |
} | |
} | |
} else if (record.type === 'attribute') { | |
unfix(record.target); | |
} | |
} | |
}); | |
// register the observer | |
// note: this will fire at least once because the body will get a childList modification | |
observer.observe(document.getElementsByTagName('body')[0], | |
{attributes: true, | |
subtree: true, | |
childList: true, | |
attributeFilter: ['style']}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment