Last active
April 18, 2021 07:05
-
-
Save RonanFelipe/25e00769ec45050d74fdec9dbb42c0c4 to your computer and use it in GitHub Desktop.
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 GitHub Timeline Handler | |
// @namespace https://gist.github.com/RonanFelipe/25e00769ec45050d74fdec9dbb42c0c4/ | |
// @version 1.0.0 | |
// @description Hide events from GitHub's Pull Request timeline. | |
// @author Eric Ribeiro and Ronan Felipe | |
// @match https://github.com/Genichiro00/blue-experiment/pull/* | |
// @match https://github.com/the-funnel/base-experiment/pull/1 | |
// @grant none | |
// @run-at document-body | |
// @downloadURL https://gist.githubusercontent.com/RonanFelipe/25e00769ec45050d74fdec9dbb42c0c4/raw/metabot_github_timeline.js | |
// @inject-into content | |
// ==/UserScript== | |
(function () { | |
'use strict'; | |
const hideDeletedTimelineEvents = () => { | |
const targetable = document.querySelectorAll(".TimelineItem.js-targetable-element"); | |
targetable.forEach(div => { | |
const deletedComent = div.innerText.includes("deleted a comment"); | |
if (deletedComent) { | |
div.remove(); | |
} | |
}); | |
} | |
const hideBotsComments = () => { | |
const timelineEvents = document.querySelectorAll(".js-timeline-item.js-timeline-progressive-focus-container"); | |
timelineEvents.forEach(event => { | |
const isBot = event.querySelector(".Label.Label--outline") != null && event.querySelector(".Label.Label--outline").innerText === "bot"; | |
if (isBot) { | |
const isMetabot = event.querySelector("a.author.Link--primary.css-truncate-target.width-fit").text === "the-funnel-bot"; | |
if (!isMetabot) { | |
event.remove(); | |
} | |
} | |
}); | |
} | |
const deleteElementsFromTimeline = () => { | |
// removing deleted comments events from timeline. | |
hideDeletedTimelineEvents(); | |
// removing bot's comments since they don't go away unless the page is refreshed | |
hideBotsComments(); | |
} | |
const saveEvent = async (event) => { | |
console.log(event); | |
const response = await fetch("https://metabot-express.herokuapp.com/events", { | |
method: 'POST', | |
mode: 'cors', | |
cache: 'no-cache', | |
credentials: 'same-origin', | |
redirect: 'follow', | |
referrerPolicy: 'no-referrer', | |
headers: { | |
"Content-type": "application/json" | |
}, | |
body: JSON.stringify(event) | |
}); | |
console.log(response); | |
} | |
const getUserName = () => { | |
return document.querySelector('meta[name="octolytics-actor-login"]')?.content; | |
} | |
const shouldSaveOnDatabase = (el) => { | |
return (el.target.tagName.toLowerCase() === "summary" && !el.target.innerText.includes("View Unchanged")) | |
|| (el.target.parentElement.tagName.toLowerCase() === "summary" && !el.target.parentElement.innerText.includes("View Unchanged")) | |
}; | |
window.addEventListener('load', function () { | |
const userName = getUserName(); | |
deleteElementsFromTimeline(); | |
if (!userName) { | |
alert("Você precisa estar logado para realizar este experimento!"); | |
} | |
document.querySelectorAll(".js-discussion")[0] | |
.addEventListener('DOMNodeInserted', deleteElementsFromTimeline, false); | |
let timelineNodeElements = document.querySelectorAll(".js-timeline-item.js-timeline-progressive-focus-container"); | |
let metabotComment; | |
timelineNodeElements.forEach(element => { | |
if (element.querySelector(".author")?.href === "https://github.com/apps/the-funnel-bot") { | |
metabotComment = element; | |
} | |
}); | |
metabotComment.addEventListener("click", function (el) { | |
if (shouldSaveOnDatabase(el)) { | |
const userName = getUserName(); | |
if (!userName) { | |
alert("Você precisa estar logado para realizar este experimento!"); | |
return; | |
} | |
const event = { | |
clickedAt: el.target.innerText, | |
timeStamp: new Date().getTime(), | |
user: userName, | |
pullRequest: window.location.pathname | |
}; | |
saveEvent(event); | |
} | |
}); | |
}, false); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment