Skip to content

Instantly share code, notes, and snippets.

@RonanFelipe
Last active April 18, 2021 07:05
Show Gist options
  • Save RonanFelipe/25e00769ec45050d74fdec9dbb42c0c4 to your computer and use it in GitHub Desktop.
Save RonanFelipe/25e00769ec45050d74fdec9dbb42c0c4 to your computer and use it in GitHub Desktop.
// ==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