diff --git a/extension/content.js b/extension/content.js index 572a66f..435b322 100644 --- a/extension/content.js +++ b/extension/content.js @@ -1,32 +1,37 @@ function freeUrl(url) { - const urlObj = new URL(url) + const urlObj = new URL(url); urlObj.host = "freedium-mirror.cfd"; return urlObj.href; } -document.addEventListener("readystatechange", (event) => { - const button = document.createElement("button"); - button.id = "free-medium__button"; - button.textContent = "Read for free"; - button.style.position = "fixed"; - button.style.bottom = "20px"; - button.style.right = "20px"; - button.style.padding = "20px 28px"; - button.style.backgroundColor = "#007bff"; - button.style.color = "#fff"; - button.style.border = "none"; - button.style.borderRadius = "48px"; - button.style.fontSize = "18px"; - button.style.cursor = "pointer"; - button.style.zIndex = "9999"; - button.style.boxShadow = "0 2px 5px rgba(0, 0, 0, 0.2)"; +if (typeof document !== "undefined") { + document.addEventListener("readystatechange", (event) => { + const button = document.createElement("button"); + button.id = "free-medium__button"; + button.textContent = "Read for free"; + button.style.position = "fixed"; + button.style.bottom = "20px"; + button.style.right = "20px"; + button.style.padding = "20px 28px"; + button.style.backgroundColor = "#007bff"; + button.style.color = "#fff"; + button.style.border = "none"; + button.style.borderRadius = "48px"; + button.style.fontSize = "18px"; + button.style.cursor = "pointer"; + button.style.zIndex = "9999"; + button.style.boxShadow = "0 2px 5px rgba(0, 0, 0, 0.2)"; - button.addEventListener("click", function () { - window.location.href = freeUrl(document.URL); - }); + button.addEventListener("click", function () { + window.location.href = freeUrl(document.URL); + }); - document.body.appendChild(button); -}); + document.body.appendChild(button); + }); +} +if (typeof module !== "undefined" && module.exports) { + module.exports = { freeUrl }; +} diff --git a/tests/unit/freeUrl.test.js b/tests/unit/freeUrl.test.js index 5e9f433..42ed544 100644 --- a/tests/unit/freeUrl.test.js +++ b/tests/unit/freeUrl.test.js @@ -1,25 +1,18 @@ -function freeUrl(url) { - const urlObj = new URL(url) - - urlObj.host = "freedium-mirror.cfd"; - - return urlObj.href; -} - +const { freeUrl } = require("../../extension/content.js"); describe("freeUrl", () => { it("converts the URLs correctly", () => { expect(freeUrl("https://medium.com/my-post")).toBe( - "https://freedium-mirror.cfd/my-post" + "https://freedium-mirror.cfd/my-post", ); expect(freeUrl("https://www.medium.com/my-post")).toBe( - "https://freedium-mirror.cfd/my-post" + "https://freedium-mirror.cfd/my-post", ); expect(freeUrl("https://some-subdomain.medium.com/my-post")).toBe( - "https://freedium-mirror.cfd/my-post" - ); - expect(freeUrl("https://medium.com/hackernoon/hello-world-79436a73e443")).toBe( - "https://freedium-mirror.cfd/hackernoon/hello-world-79436a73e443" + "https://freedium-mirror.cfd/my-post", ); + expect( + freeUrl("https://medium.com/hackernoon/hello-world-79436a73e443"), + ).toBe("https://freedium-mirror.cfd/hackernoon/hello-world-79436a73e443"); }); });