Created
August 17, 2018 21:08
-
-
Save gpsarkar/ac115dd9b55ba24554cd6b73b7009854 to your computer and use it in GitHub Desktop.
sfdry mcq practice - greasemonkey script
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 SanFoundaryPractice | |
// @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:none; | |
} | |
.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; | |
} | |
.toggle-link { | |
display: block; | |
padding: 2px 10px; | |
background: #E4F1FE; | |
color: #4183D7; | |
position: fixed; | |
right: -4px; | |
top: 100px; | |
cursor: pointer; | |
font-size: 21px; | |
line-height: 45px; | |
border-radius: 7px; | |
overflow: hidden; | |
} | |
.toggle-link2 { | |
display: block; | |
padding: 2px 10px; | |
background: #E4F1FE; | |
color: #4183D7; | |
position: fixed; | |
right: -4px; | |
top: 150px; | |
cursor: pointer; | |
font-size: 21px; | |
line-height: 45px; | |
border-radius: 7px; | |
overflow: hidden; | |
} | |
` ); | |
// 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(); | |
// Add a toggle explanation button | |
var toggleExplanationNode = document.createElement('a'); | |
toggleExplanationNode.innerText = "Show explanations"; | |
toggleExplanationNode.classList.add('toggle-link'); | |
toggleExplanationNode.onclick = function() { | |
document.querySelectorAll("[id*=target]").forEach(e => { | |
if(e.classList.contains('explanation')) { | |
e.classList.remove('explanation') | |
} else { | |
e.classList.add('explanation') | |
} | |
}) | |
}; | |
document.querySelector("#content").appendChild(toggleExplanationNode); | |
// Add a button to highlight answer | |
var toggleAnswerNode = document.createElement('a'); | |
toggleAnswerNode.innerText = "Show Answers"; | |
toggleAnswerNode.classList.add('toggle-link2'); | |
toggleAnswerNode.onclick = function() { | |
highlightAnswer(); | |
}; | |
document.querySelector("#content").appendChild(toggleAnswerNode); | |
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() { | |
// hide 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); | |
} | |
}); | |
// Show checkboxes | |
showCheckbox(); | |
}) | |
// 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; | |
} | |
}) | |
}) | |
function highlightAnswer() { | |
// highlight the answer | |
var highligtedAnswers = document.querySelectorAll(".selected-answer"); | |
console.log(highligtedAnswers); | |
if(highligtedAnswers && highligtedAnswers.length > 0) { | |
highligtedAnswers.forEach((e,i) => { | |
console.log(e); | |
if(e.classList.contains('answer-highlighted')) { | |
e.classList.remove('answer-highlighted') | |
} else { | |
e.classList.add('answer-highlighted') | |
} | |
}); | |
} else { | |
console.log("highlighting"); | |
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) => { | |
e.previousSibling.childNodes | |
.forEach((c, j) => { | |
// 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'); | |
replacementNode.classList.add('selected-answer'); | |
} | |
}) | |
}) | |
} | |
} | |
// 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