Skip to content

Instantly share code, notes, and snippets.

@hyperpape
Created September 14, 2012 20:10
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 hyperpape/3724409 to your computer and use it in GitHub Desktop.
Save hyperpape/3724409 to your computer and use it in GitHub Desktop.
Show/hide Hidden Text
{
"name": "Show/Hide Hidden Text",
"version": "1",
"manifest_version": 2,
"description": "Shows and hides all text hidden in hide tags.",
"content_scripts": [
{"matches" : ["http://lifein19x19.com/*",
"http://www.lifein19x19.com/*"],
"js": ["showhide.js"]
}
]
}
// Create a link that shows or hides all hidden comments on an
// lifein19x19.com page. It is designed to work in IE8 or higher, and
// use capability testing to be unobtrusive in older browsers.
// Add a link to the header, to the left of "Previous topic"
function addLink() {
var headerTarget = document.querySelectorAll('.nav')[2];
// create a show/hide link and add event listener
var showHideLink = document.createElement('a');
showHideLink.setAttribute('href', 'about:blank');
showHideLink.id = 'showHide';
if (showHideLink.addEventListener) {
showHideLink.addEventListener('click', showhide, false);
} else {
showHideLink.attachEvent('click', showhide);
}
// insert show/hide link in the page
showHideLink.appendChild(document.createTextNode('Show hidden text'));
headerTarget.insertBefore(showHideLink,
headerTarget.getElementsByTagName('a')[0]);
// insert a '|' as a vertical divider
var bar = document.createTextNode(' | ');
headerTarget.insertBefore(bar,
headerTarget.getElementsByTagName('a')[1]);
trackStatus();
}
function trackStatus () {
var buttons = localShowHideButtons();
if (buttons.length && buttons[0].addEventListener) {
for (var i = 0, ilen = buttons.length; i < ilen; i++) {
buttons[i].onclick = null;
buttons[i].addEventListener('click', toggleVisibility, false);
}
} else {
for (var i = 0, ilen = buttons.length; i < ilen; i++) {
buttons[i].onclick = toggleVisibility;
}
}
}
// Return true if any hidden comments have been shown
function visible () {
var buttons = localShowHideButtons();
for (var i = 0, ilen = buttons.length; i < ilen; i++) {
if (buttonTarget(buttons[i]).style.display !== 'none') {
return true;
}
}
return false;
}
// Update the text of the show/hide control depending on which action
// it will perform.
function checkStatus () {
var showHide = document.getElementById('showHide');
if (!visible()) {
var newText = 'Show hidden text';
} else {
newText = 'Hide private comments';
}
if (showHide.textContent) {
showHide.textContent = newText;
} else if (showHide.innerText) {
showHide.innerText = newText;
}
}
function show (button) {
buttonTarget(button).style.display = '';
button.textContent = '';
button.value = 'Hide';
}
function hide (button) {
buttonTarget(button).style.display = 'none';
button.textContent = '';
button.value = 'Show';
}
function toggleVisibility () {
if (buttonTarget(this).style.display === 'none') {
show(this);
} else {
hide(this);
}
checkStatus();
}
// Show/hide text within hide tags.
function showhide (event) {
var hidebuttons = localShowHideButtons();
if (!visible()) {
for (var i = 0, ilen = hidebuttons.length; i < ilen; i++) {
show(hidebuttons[i]);
}
} else {
for (i = 0, ilen = hidebuttons.length; i < ilen; i++) {
hide(hidebuttons[i]);
}
}
checkStatus();
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
}
// Get the show/hide buttons for all individual comments
function localShowHideButtons () {
return document.querySelectorAll('.button2');
}
// Given a show/hide button, this returns the element whose
// style.display should be toggled
function buttonTarget (hidebutton) {
var ancestor = hidebutton.parentNode.parentNode;
return ancestor.querySelectorAll('.quotecontent')[0].
getElementsByTagName('div')[0];
}
// Capability testing.
if (document.querySelectorAll) {
if (window.addEventListener) {
window.addEventListener('load', addLink, false);
} else if (window.attachEvent) {
window.attachEvent('load', addLink);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment