Skip to content

Instantly share code, notes, and snippets.

@mino0123
Last active November 25, 2016 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mino0123/17b0b1ef8c323baeb9e41219ab95f311 to your computer and use it in GitHub Desktop.
Save mino0123/17b0b1ef8c323baeb9e41219ab95f311 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Qiita_TOC_to_content
// @namespace mino0123
// @include http://qiita.com/*/items/*
// @include http://qiita.com/*/private/*
// @version 1
// @grant none
// ==/UserScript==
const subColumnEl = document.getElementsByClassName('col-sm-3')[0];
if (subColumnEl) {
const observer = new MutationObserver((mutations) => {
const tocTreeEl = document.getElementsByClassName('tocTree')[0];
const contentEl = document.querySelector('.markdownContent.clearfix');// avoid slide
const hasTocItem = tocTreeEl.getElementsByTagName('li').length > 0;
if (tocTreeEl && contentEl && hasTocItem) {
// Disconnect observer
observer.disconnect();
// Copy toc to content
const cloneToc = tocTreeEl.cloneNode(true);
cloneToc.className += ' tocToContent';
contentEl.insertBefore(cloneToc, contentEl.firstChild);
// Add styles
const styleEl = document.createElement('style');
styleEl.type = 'text/css';
styleEl.innerHTML = `
.tocToContent {
width: 100% !important;
border: 1px solid #CCC;
font-size: 70%;
}
.tocToContent a {
display: inline !important;
}
`;
document.head.appendChild(styleEl);
}
});
observer.observe(subColumnEl, { childList: true, subtree: true });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment