Skip to content

Instantly share code, notes, and snippets.

@kntjspr
Created January 27, 2026 17:42
Show Gist options
  • Select an option

  • Save kntjspr/4c3207dfa8e9422f7407eb91ccce9056 to your computer and use it in GitHub Desktop.

Select an option

Save kntjspr/4c3207dfa8e9422f7407eb91ccce9056 to your computer and use it in GitHub Desktop.
Social media blocker userscript
// ==UserScript==
// @name Social Media Blocker - Instagram Reels & Facebook Newsfeed
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Blocks Instagram Reels and Facebook Newsfeed with temporary disable option
// @author https://github.com/kntjspr
// @match https://www.instagram.com/*
// @match https://instagram.com/*
// @match https://www.facebook.com/*
// @match https://facebook.com/*
// @grant GM_setValue
// @grant GM_getValue
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
const STORAGE_KEY = 'socialMediaBlocker_disabled';
const DISABLE_DURATION = 5 * 60 * 1000; // 5 minutes in milliseconds
// Check if blocker is temporarily disabled
function isBlockerDisabled() {
const disabledUntil = GM_getValue(STORAGE_KEY, 0);
return Date.now() < disabledUntil;
}
// Disable blocker for 5 minutes
function disableBlocker() {
const disableUntil = Date.now() + DISABLE_DURATION;
GM_setValue(STORAGE_KEY, disableUntil);
location.reload();
}
// Format time remaining
function formatTimeRemaining() {
const disabledUntil = GM_getValue(STORAGE_KEY, 0);
const remaining = disabledUntil - Date.now();
if (remaining <= 0) return null;
const minutes = Math.floor(remaining / 60000);
const seconds = Math.floor((remaining % 60000) / 1000);
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
}
// Create blocking overlay
function createBlockOverlay(message, buttonText = 'Disable for 5 minutes') {
const overlay = document.createElement('div');
overlay.id = 'social-blocker-overlay';
overlay.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
justify-content: center;
align-items: center;
z-index: 999999;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
`;
const content = document.createElement('div');
content.style.cssText = `
background: white;
padding: 40px 50px;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
text-align: center;
max-width: 500px;
`;
const icon = document.createElement('div');
icon.innerHTML = '🚫';
icon.style.cssText = `
font-size: 64px;
margin-bottom: 20px;
`;
const title = document.createElement('h1');
title.textContent = 'Content Blocked';
title.style.cssText = `
color: #333;
font-size: 32px;
margin: 0 0 15px 0;
font-weight: 700;
`;
const description = document.createElement('p');
description.textContent = message;
description.style.cssText = `
color: #666;
font-size: 18px;
margin: 0 0 30px 0;
line-height: 1.6;
`;
const button = document.createElement('button');
button.textContent = buttonText;
button.style.cssText = `
background: #667eea;
color: white;
border: none;
padding: 15px 30px;
font-size: 16px;
border-radius: 10px;
cursor: pointer;
font-weight: 600;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
`;
button.onmouseover = () => {
button.style.background = '#5568d3';
button.style.transform = 'translateY(-2px)';
button.style.boxShadow = '0 6px 20px rgba(102, 126, 234, 0.6)';
};
button.onmouseout = () => {
button.style.background = '#667eea';
button.style.transform = 'translateY(0)';
button.style.boxShadow = '0 4px 15px rgba(102, 126, 234, 0.4)';
};
button.onclick = disableBlocker;
content.appendChild(icon);
content.appendChild(title);
content.appendChild(description);
content.appendChild(button);
overlay.appendChild(content);
return overlay;
}
// Create timer badge (shows when blocker is temporarily disabled)
function createTimerBadge() {
const timeRemaining = formatTimeRemaining();
if (!timeRemaining) return null;
const badge = document.createElement('div');
badge.id = 'blocker-timer-badge';
badge.style.cssText = `
position: fixed;
bottom: 20px;
left: 20px;
background: rgba(102, 126, 234, 0.95);
color: white;
padding: 12px 20px;
border-radius: 25px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
font-size: 14px;
font-weight: 600;
z-index: 999998;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
display: flex;
align-items: center;
gap: 12px;
`;
const timerContent = document.createElement('div');
timerContent.style.cssText = `
display: flex;
align-items: center;
gap: 8px;
`;
timerContent.innerHTML = `
<span>⏱️</span>
<span id="timer-text">Blocker disabled: ${timeRemaining}</span>
`;
const skipButton = document.createElement('button');
skipButton.textContent = 'Block Now';
skipButton.style.cssText = `
background: rgba(255, 255, 255, 0.2);
color: white;
border: 1px solid rgba(255, 255, 255, 0.3);
padding: 6px 12px;
border-radius: 15px;
cursor: pointer;
font-size: 12px;
font-weight: 600;
transition: all 0.2s ease;
`;
skipButton.onmouseover = () => {
skipButton.style.background = 'rgba(255, 255, 255, 0.3)';
};
skipButton.onmouseout = () => {
skipButton.style.background = 'rgba(255, 255, 255, 0.2)';
};
skipButton.onclick = () => {
GM_setValue(STORAGE_KEY, 0);
location.reload();
};
badge.appendChild(timerContent);
badge.appendChild(skipButton);
// Update timer every second
const timerInterval = setInterval(() => {
const time = formatTimeRemaining();
if (!time) {
clearInterval(timerInterval);
location.reload();
} else {
const timerText = document.getElementById('timer-text');
if (timerText) {
timerText.textContent = `Blocker disabled: ${time}`;
}
}
}, 1000);
return badge;
}
// Block Instagram Feed and Reels
function blockInstagram() {
// Check if we're on a page that should NOT be blocked
const checkShouldBlock = () => {
const path = window.location.pathname;
const isDirectMessages = path.includes('/direct');
const isProfile = path.match(/^\/[^/]+\/?$/); // matches /@username or /username
const isExplore = path.includes('/explore');
const isSearch = path.includes('/search');
const isReelsPage = path.includes('/reels');
const isHomeFeed = path === '/' || path === '';
return {
shouldBlock: (isReelsPage || isHomeFeed) && !isDirectMessages,
isReelsPage,
isHomeFeed,
isDirectMessages
};
};
const applyBlocking = () => {
const { shouldBlock, isReelsPage, isHomeFeed, isDirectMessages } = checkShouldBlock();
// If we're on DMs or other allowed pages, remove any existing overlays
if (isDirectMessages || !shouldBlock) {
const existingOverlay = document.getElementById('social-blocker-overlay');
if (existingOverlay) {
existingOverlay.remove();
}
// Show children again
const mains = document.querySelectorAll('main');
mains.forEach(main => {
Array.from(main.children).forEach(child => {
if (child.id !== 'social-blocker-overlay') {
child.style.display = '';
}
});
});
return;
}
// Check if blocker is disabled
if (isBlockerDisabled()) {
// Show timer badge
const existingBadge = document.getElementById('blocker-timer-badge');
if (!existingBadge) {
const badge = createTimerBadge();
if (badge) document.body.appendChild(badge);
}
return;
}
if (isReelsPage) {
// Block entire reels page
if (!document.getElementById('social-blocker-overlay')) {
document.body.innerHTML = '';
const overlay = createBlockOverlay(
'Instagram Reels is blocked to help you stay focused. Take a break and come back later!',
'Disable for 5 minutes'
);
document.body.appendChild(overlay);
}
} else if (isHomeFeed) {
// Block the home feed
const blockFeed = () => {
// Main feed container selectors for Instagram
const feedSelectors = [
'main[role="main"]',
'main'
];
feedSelectors.forEach(selector => {
const feeds = document.querySelectorAll(selector);
feeds.forEach(feed => {
if (feed && !feed.querySelector('#social-blocker-overlay')) {
feed.style.position = 'relative';
feed.style.minHeight = '100vh';
// Hide original content
Array.from(feed.children).forEach(child => {
if (child.id !== 'social-blocker-overlay') {
child.style.display = 'none';
}
});
// Add blocking overlay
const overlay = createBlockOverlay(
'Instagram Feed is blocked to help you stay productive. Use Instagram for specific purposes only!',
'Disable for 5 minutes'
);
overlay.style.position = 'absolute';
feed.appendChild(overlay);
}
});
});
};
blockFeed();
}
// Hide reels links throughout the interface (but not other navigation)
const hideLinks = () => {
const reelsLinks = document.querySelectorAll('a[href*="/reels"]');
reelsLinks.forEach(link => {
link.style.pointerEvents = 'none';
link.style.opacity = '0.5';
});
};
hideLinks();
};
// Initial check
applyBlocking();
// Listen for URL changes (for navigation without page reload)
let lastPath = window.location.pathname;
setInterval(() => {
if (window.location.pathname !== lastPath) {
lastPath = window.location.pathname;
applyBlocking();
}
}, 500);
// Observe for dynamic content changes
const observer = new MutationObserver(() => {
const { shouldBlock } = checkShouldBlock();
if (shouldBlock && !isBlockerDisabled()) {
applyBlocking();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
// Periodic check for reels links
setInterval(() => {
const { shouldBlock } = checkShouldBlock();
if (!shouldBlock || isBlockerDisabled()) return;
const reelsLinks = document.querySelectorAll('a[href*="/reels"]');
reelsLinks.forEach(link => {
link.style.pointerEvents = 'none';
link.style.opacity = '0.5';
});
}, 1000);
}
// Block Facebook Newsfeed
function blockFacebookNewsfeed() {
if (isBlockerDisabled()) {
// Show timer badge
const existingBadge = document.getElementById('blocker-timer-badge');
if (!existingBadge) {
const badge = createTimerBadge();
if (badge) document.body.appendChild(badge);
}
return;
}
let overlayAdded = false;
const blockNewsfeed = () => {
if (overlayAdded) return;
// Find the main body content area (everything below the navbar)
const bodyElement = document.body;
if (bodyElement && !document.getElementById('facebook-page-blocker')) {
// Create a full-page overlay that covers everything except the navbar
const overlay = document.createElement('div');
overlay.id = 'facebook-page-blocker';
overlay.style.cssText = `
position: fixed;
top: 56px;
left: 0;
width: 100%;
height: calc(100vh - 56px);
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
justify-content: center;
align-items: center;
z-index: 999999;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
`;
const content = document.createElement('div');
content.style.cssText = `
background: white;
padding: 40px 50px;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
text-align: center;
max-width: 500px;
`;
const icon = document.createElement('div');
icon.innerHTML = '🚫';
icon.style.cssText = `
font-size: 64px;
margin-bottom: 20px;
`;
const title = document.createElement('h1');
title.textContent = 'Facebook Blocked';
title.style.cssText = `
color: #333;
font-size: 32px;
margin: 0 0 15px 0;
font-weight: 700;
`;
const description = document.createElement('p');
description.textContent = 'Facebook is blocked to help you stay productive. You can still access your notifications and messages from the top bar!';
description.style.cssText = `
color: #666;
font-size: 18px;
margin: 0 0 30px 0;
line-height: 1.6;
`;
const button = document.createElement('button');
button.textContent = 'Disable for 5 minutes';
button.style.cssText = `
background: #667eea;
color: white;
border: none;
padding: 15px 30px;
font-size: 16px;
border-radius: 10px;
cursor: pointer;
font-weight: 600;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
`;
button.onmouseover = () => {
button.style.background = '#5568d3';
button.style.transform = 'translateY(-2px)';
button.style.boxShadow = '0 6px 20px rgba(102, 126, 234, 0.6)';
};
button.onmouseout = () => {
button.style.background = '#667eea';
button.style.transform = 'translateY(0)';
button.style.boxShadow = '0 4px 15px rgba(102, 126, 234, 0.4)';
};
button.onclick = disableBlocker;
content.appendChild(icon);
content.appendChild(title);
content.appendChild(description);
content.appendChild(button);
overlay.appendChild(content);
bodyElement.appendChild(overlay);
overlayAdded = true;
console.log('Facebook page blocked successfully');
}
// Also ensure the navbar remains interactive
const navbar = document.querySelector('[role="banner"]') ||
document.querySelector('header') ||
document.querySelector('[class*="navbar"]');
if (navbar) {
navbar.style.position = 'relative';
navbar.style.zIndex = '9999999';
}
};
// Try blocking immediately
blockNewsfeed();
// Wait a bit and try again (for late-loading content)
setTimeout(blockNewsfeed, 500);
setTimeout(blockNewsfeed, 1000);
setTimeout(blockNewsfeed, 2000);
// Use MutationObserver to catch dynamic content
const observer = new MutationObserver(() => {
if (!overlayAdded) {
blockNewsfeed();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
// Periodic backup check
setInterval(() => {
// Check if overlay was removed
if (overlayAdded && !document.getElementById('facebook-page-blocker')) {
overlayAdded = false;
blockNewsfeed();
} else if (!overlayAdded) {
blockNewsfeed();
}
}, 2000);
}
// Initialize based on current site
function init() {
const hostname = window.location.hostname;
if (hostname.includes('instagram.com')) {
// Wait for DOM to be ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', blockInstagram);
} else {
blockInstagram();
}
} else if (hostname.includes('facebook.com')) {
// Wait for DOM to be ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', blockFacebookNewsfeed);
} else {
blockFacebookNewsfeed();
}
}
}
// Start the script
init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment