Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
felps-dev committed Jul 9, 2024
1 parent f494199 commit 4e098b7
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
5 changes: 5 additions & 0 deletions clown.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h1>Hey!</h1>
</body>
</html>
Binary file added clown.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "Crynchycomments",
"description": "A simple disqus wrapper for crunchyroll",
"version": "0.5",
"manifest_version": 3,
"action": {
"default_popup": "clown.html",
"default_icon": "clown.png"
},
"content_scripts": [
{
"js": [
"scripts/comments.js"
],
"matches": [
"https://www.crunchyroll.com/*"
],
"world": "MAIN"
}
]
}
99 changes: 99 additions & 0 deletions scripts/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

// First, override the history.pushState and history.replaceState methods
(function (history) {
var pushState = history.pushState;
var replaceState = history.replaceState;

history.pushState = function (state) {
if (typeof history.onpushstate == "function") {
history.onpushstate({ state: state });
}
// call the original pushState method
return pushState.apply(history, arguments);
};

history.replaceState = function (state) {
if (typeof history.onreplacestate == "function") {
history.onreplacestate({ state: state });
}
// call the original replaceState method
return replaceState.apply(history, arguments);
};
})(window.history);

function loadCrunchyComments() {
const mediaWrapper = document.querySelector(".erc-current-media-info");
if (!mediaWrapper) return;

// Remove any existing diqus_thread div
const existingDisqusThread = document.querySelector("#disqus_thread");
if (existingDisqusThread) {
existingDisqusThread.remove();
}

// Add div with id "disqus_thread" to the mediaWrapper
const disqusThread = document.createElement("div");
disqusThread.id = "disqus_thread";
mediaWrapper.appendChild(disqusThread);

// Load Disqus comments
(function () {
var d = document,
s = d.createElement("script");
s.src = "https://crunchycomments.disqus.com/embed.js";
s.setAttribute("data-timestamp", +new Date());
(d.head || d.body).appendChild(s);
})();
}

async function waitForMediaWrapper() {
while (true) {
if (document.querySelector(".erc-current-media-info")) {
loadCrunchyComments();
return;
}
await sleep(1000);
}
}

// Then, add event listeners for these events
window.history.onpushstate = function (event) {
waitForMediaWrapper();
};
window.history.onreplacestate = function (event) {
waitForMediaWrapper();
};

// Don't forget to handle the popstate event as well
window.onpopstate = function (event) {
waitForMediaWrapper();
};

async function ensureDisqusIsLastElement() {
while (true) {
const mediaWrapper = document.querySelector(".erc-current-media-info");
if (!mediaWrapper) {
await sleep(1000);
continue;
}

const disqusThread = document.querySelector("#disqus_thread");
if (!disqusThread) {
await sleep(1000);
continue;
}
const lastChild = mediaWrapper.lastElementChild;

if (lastChild !== disqusThread) {
mediaWrapper.appendChild(disqusThread);
}

await sleep(1000);
}
}

waitForMediaWrapper();
ensureDisqusIsLastElement();

0 comments on commit 4e098b7

Please sign in to comment.