(function() {
    var element = null;
    var nav_elements = document.querySelectorAll('.nav-item-container');

    for (var i = 0; i < nav_elements.length; i++) {
        var current = nav_elements[i];
        if (current.innerHTML == 'My Library') {
            element = current;
            break;
        }
    }

    if (element != null) {
        chrome.storage.sync.get({'gpmDefaultLibraryView': 'albums'}, function(item) {
            element.setAttribute('data-type', item.gpmDefaultLibraryView);
        });
    } else {
        console.error('No element found; did Google change the page?');
    }
})();

(function() {

    function findTarget(element) {
        if (element.nodeType === 1) {
            var candidates = element.querySelectorAll('.nav-item-container');
            for (var i = 0; i < candidates.length; i++) {
                if (candidates[i].textContent == 'My Library') {
                    return candidates[i];
                }
            }
        }
    }

    function modifyTarget(element) {
        chrome.storage.sync.get({'gpmDefaultLibraryView': 'albums'}, function(item) {
            element.setAttribute('data-type', item.gpmDefaultLibraryView);
        });
    }

    var observer = new MutationObserver(function(mutations, observer) {
        mutations.forEach(function(mutation) {
            for (var i = 0; i < mutation.addedNodes.length; i++) {
                var element = findTarget(mutation.addedNodes[i]);
                if (element) {
                    modifyTarget(element);
                    observer.disconnect();
                }
            }
        });
    });
    observer.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: false,
        characterData: false
    });

})();

Handle dynamic insertion of page elements