Skip to content

Instantly share code, notes, and snippets.

@lionicsheriff
Last active October 10, 2015 18:57
Show Gist options
  • Save lionicsheriff/3735718 to your computer and use it in GitHub Desktop.
Save lionicsheriff/3735718 to your computer and use it in GitHub Desktop.
unfix position
// ==UserScript==
// @id dft-postion-unfixed
// @name Remove position fixed
// @version 2.0
// @namespace dft
// @author Matthew Goodall
// @description Changes position:fixed to position:static
// @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 element 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