Last active
August 9, 2018 19:10
-
-
Save gpsarkar/19888bf7e042c707d25c64619829110e to your computer and use it in GitHub Desktop.
greasemonkey script for removing ads from a site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name SanFoundaryRemoveAds | |
// @version 1 | |
// @grant GM_util | |
// @match https://www.sanfoundry.com/* | |
// @author SRKR | |
// ==/UserScript== | |
// Styles function - needs to be added in GM 4 | |
function GM_addStyle (cssStr) { | |
var D = document; | |
var newNode = D.createElement ('style'); | |
newNode.textContent = cssStr; | |
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement; | |
targ.appendChild (newNode); | |
} | |
// Define styles | |
GM_addStyle ( ` | |
.xxxxxx p::first-line { | |
font-weight:bold; | |
font-size:large; | |
} | |
.entry-content { | |
margin:3rem !important; | |
} | |
.answer-highlighted { | |
color: #446CB3 !important; | |
font-weight:bold; | |
} | |
.explanation { | |
border-bottom: 2px solid #ececec; | |
margin-bottom: 2rem; | |
display:block; | |
} | |
.question-highlighted { | |
font-weight:bold; | |
font-size:14px; | |
display:block; | |
} | |
.question-indent-left { | |
margin-left:-22px; | |
} | |
.answer-spacer { | |
display:block; | |
} | |
.entry-summary li { | |
list-style:none !important; | |
} | |
.entry-summary div:first-child { | |
display:none; | |
} | |
.navbar .nav > li > a { | |
font-size:10px !important; | |
padding: 1px; | |
display: unset; | |
} | |
#searchform { | |
display:none; | |
} | |
.url-visit-count { | |
display:inline-block; | |
padding: 0 5px; | |
background-color: #C8F7C5; | |
color: #1E824C; | |
font-weight: lighter; | |
font-size: x-small; | |
clip-path: circle(8px); | |
font-family: Courier New, Courier, Lucida Sans Typewriter, Lucida Typewriter, monospace; | |
} | |
` ); | |
// Remove all ads | |
document.querySelectorAll("[id*='pub-ad']").forEach(e => e.parentNode.removeChild(e)); | |
// Remove top banner | |
document.querySelectorAll("#banner").forEach(e => e.parentNode.removeChild(e)); | |
// Remove the right side bar | |
document.querySelectorAll("#secondary").forEach(e => e.parentNode.removeChild(e)); | |
// Article full width | |
document.querySelector('#content').classList.remove("span6"); | |
// Remove all unnecessary posts | |
document.querySelectorAll(".sf-post").forEach(e => e.parentNode.removeChild(e)); | |
// Remove subcribe form | |
document.querySelectorAll("[class*='subscribe-form']").forEach(e => e.parentNode.removeChild(e)); | |
// Remove show answer toggle link | |
document.querySelectorAll("[title='View Answer']").forEach(e => e.parentNode.removeChild(e)); | |
// Remove social media links | |
document.querySelector(".e-mailit_bottom_toolbox").remove(); | |
// Remove course images | |
document.querySelectorAll("img[class='aligncenter']").forEach(e => e.parentNode.parentNode.removeChild(e.parentNode)); | |
// Remove footer | |
document.querySelector("#footer_widgets_wrapper").remove(); | |
document.querySelector("#after_footer").remove(); | |
function updateUrlViewCount(url) { | |
var sfVisitedUrlsCount = (JSON.parse(localStorage.getItem("sfVisitedUrlsCount"))) || []; | |
for (var i = 0; i < sfVisitedUrlsCount.length; i++) { | |
if (sfVisitedUrlsCount[i].url === url) { | |
sfVisitedUrlsCount[i].count = sfVisitedUrlsCount[i].count + 1; | |
break; | |
} | |
} | |
localStorage.setItem('sfVisitedUrlsCount', JSON.stringify(sfVisitedUrlsCount)); | |
} | |
function syncLocalStorageLists() { | |
var sfVisitedUrls = (JSON.parse(localStorage.getItem("sfVisitedUrls"))) || []; | |
var sfVisitedUrlsCount = (JSON.parse(localStorage.getItem("sfVisitedUrlsCount"))) || []; | |
if(sfVisitedUrls.length != sfVisitedUrlsCount.length) { | |
for (var i = 0; i < sfVisitedUrls.length; i++) { | |
if (!sfVisitedUrlsCount.filter(s => s.url).includes(sfVisitedUrls[i])) { | |
sfVisitedUrlsCount.unshift({url: sfVisitedUrls[i], count: 1}); | |
} | |
} | |
} | |
//localStorage.setItem('sfVisitedUrls', JSON.stringify(sfVisitedUrls)); | |
localStorage.setItem('sfVisitedUrlsCount', JSON.stringify(sfVisitedUrlsCount)); | |
} | |
// On window load | |
window.addEventListener('load', function() { | |
// Show all answers | |
document.querySelectorAll("[id*=target]").forEach(e => e.classList.add('explanation')); | |
syncLocalStorageLists(); | |
// save current page url in visited list | |
var sfVisitedUrls = (JSON.parse(localStorage.getItem("sfVisitedUrls"))) || []; | |
if(!(sfVisitedUrls.includes(window.location.href))) { | |
sfVisitedUrls.unshift(window.location.href); | |
localStorage.setItem('sfVisitedUrls', JSON.stringify(sfVisitedUrls)); | |
// save current page url in count list url as well | |
var sfVisitedUrlsCount = (JSON.parse(localStorage.getItem("sfVisitedUrlsCount"))) || []; | |
sfVisitedUrlsCount.unshift({url: window.location.href, count: 1}); | |
localStorage.setItem('sfVisitedUrlsCount', JSON.stringify(sfVisitedUrlsCount)); | |
} else { | |
// Increase the view count | |
updateUrlViewCount(window.location.href); | |
} | |
// Add the checked flag, if page is visited | |
document.querySelectorAll("a").forEach(e => { | |
if(sfVisitedUrls.includes(e.getAttribute('href'))) { | |
var visitedNode = new Image(); | |
visitedNode.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB2ElEQVQ4jX3TT4jNYRTG8c/53XsTFjMkxMJKutKYBdlpMoVsjGQWrGyUhYVC49r8mDQpO5GZFJlsbNSQv7FQdiZKKZkslJiapETo3t9rce/lDsbZnvc5Pc/3PYf/VDpiYRqyKBFzvfmrkQ6Zp8uQsm0KS2TqCp+EB+rG4rR3ne/Ls8Q1vSrGhapCQ2YaP4QqNilbiz3/dJBqepXdQrfMY4VRyZSSCnokG+OkQymXKRyXGY3cTPmX7YpxSTcuRO7YH8me42raoyS5JLNP4SnuNSN0GRKqwv1/iJsOD6hYYRwDGIhT7v1m0ATWwGjKLYDIfZ0FdrHr6MeOyD1q97J0xMIW7WnJlOQyJtqDUm6BxSbQh62d4tm/kKQW1hHJQ9xMNXsl17FOYUsMm2wNHpNM+6iWxVlfZGawXMnqyD0X+tGr4oOwRqEvhk2Cwm6FQWF9nPM9Aw13hZLkYCt/e8hLbI5hLzpc70JZ4cavPWgRfiZZpXAxhh1tphKhFa3J4zz2K7zx1oa44lt0NHtwW9IleSIzpuE1KFkpOYEe4bOGne1Is26hNeQaqigk71uAlwkl4ZXvBmPEy7bm72PKldUdV7IdS4WEGXV3lJ2J3I8/NXNWOmx+ex/mqp/EUKUjV/Hx8QAAAABJRU5ErkJggg=="; | |
e.parentNode.insertBefore(visitedNode, e); | |
} | |
}); | |
// Add the count info | |
var sfVisitedUrlsCount = (JSON.parse(localStorage.getItem("sfVisitedUrlsCount"))) || []; | |
document.querySelectorAll("a").forEach(e => { | |
var item = sfVisitedUrlsCount.find(s => s.url === e.getAttribute('href')); | |
if(item) { | |
var countNode = document.createElement('span'); | |
countNode.innerHTML = item.count; | |
countNode.classList.add('url-visit-count'); | |
e.parentNode.insertBefore(countNode, e); | |
} | |
}); | |
}) | |
// highlight the answer | |
var targets = document.querySelectorAll("[id*='target']"); | |
var answers = Array.from(targets).map(e => { return e.firstChild.textContent.replace(/Answer\s*:\s*/g,"").trim(); }); | |
targets | |
.forEach((e,i) => { | |
var isQuestionBlock = true; | |
var isAnswerBlock = false; | |
var gapSet = false; | |
e.previousSibling.childNodes | |
.forEach((c, j) => { | |
if(c.textContent.trim().toLowerCase().startsWith('a)') || c.textContent.trim().toLowerCase().startsWith('(a)')) { | |
isQuestionBlock = false; | |
isAnswerBlock = true; | |
} | |
// highlight the question | |
if(isQuestionBlock) { | |
var questionNode = document.createElement('span'); | |
// If image tag then grab the image, else add text, (image is under a link actually) | |
questionNode.innerHTML = (c.tagName === 'A') ? c.innerHTML : c.textContent; | |
c.parentNode.insertBefore(questionNode, c); | |
c.parentNode.removeChild(c); | |
questionNode.classList.add('question-highlighted'); | |
// if first line then move a bit more left | |
if(j === 0) { | |
questionNode.classList.add('question-indent-left'); | |
} | |
} | |
// mind the gap | |
if(isAnswerBlock && !gapSet) { | |
var gapNode = document.createElement('span'); | |
gapNode.innerHTML = " "; | |
c.parentNode.insertBefore(gapNode, c); | |
gapNode.classList.add('answer-spacer'); | |
gapSet = true; | |
} | |
// highlight the correct answer | |
if(c.textContent.trim().toLowerCase().startsWith(answers[i]) || c.textContent.trim().toLowerCase().startsWith(`(${answers[i]}`)) { | |
var replacementNode = document.createElement('span'); | |
replacementNode.innerHTML = c.textContent; | |
c.parentNode.insertBefore(replacementNode, c); | |
c.parentNode.removeChild(c); | |
//replacementNode.style.fontWeight = 'bold'; | |
replacementNode.classList.add('answer-highlighted'); | |
} | |
}) | |
}) | |
// Do the same in case of code blocks in question | |
var codeBlocks = document.querySelectorAll(".hk1_style-wrap5"); | |
codeBlocks | |
.forEach((e,i) => { | |
e.previousElementSibling.childNodes | |
.forEach((c, j) => { | |
// highlight the question | |
var questionNode = document.createElement('span'); | |
questionNode.innerHTML = c.textContent; | |
c.parentNode.insertBefore(questionNode, c); | |
c.parentNode.removeChild(c); | |
questionNode.classList.add('question-highlighted'); | |
// if first line then move a bit more left | |
if(j === 0) { | |
questionNode.classList.add('question-indent-left'); | |
} | |
}) | |
}) | |
// Happy browsing and study well. No distractions anymore. | |
// For any issues drop a mail at testautomation0101/gmail/com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment