let refreshIntervalId; let currentInterval = 15000; // Default, will be updated by background script let userScrolledDown = false; let scrollThreshold = window.innerHeight * 0.05; // Threshold as 5% of the viewport height window.addEventListener('scroll', () => { if (window.scrollY > scrollThreshold) { userScrolledDown = true; } else { userScrolledDown = false; } }); function simulateHomeButtonClick() { // Check if we are on the main Bluesky feed page if (window.location.href === 'https://bsky.app/') { // Find any element with aria-label="Home" // This is more robust than looking for a specific tag like 'a' const homeButton = document.querySelector('[aria-label="Home"]'); // Only click the "Home" button (to refresh) if the user has not scrolled down if (!userScrolledDown) { if (homeButton) { // Check if the element is visible and clickable const rect = homeButton.getBoundingClientRect(); const isVisible = rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth) && rect.width > 0 && rect.height > 0; if (isVisible && typeof homeButton.click === 'function') { homeButton.click(); console.log("Bluesky Auto Refresh: Simulated click on the 'Home' button (at top of timeline)."); userScrolledDown = false; // Reset scroll status after refresh } else if (!isVisible) { console.log("Bluesky Auto Refresh: 'Home' button found but not visible/clickable."); } else { console.log("Bluesky Auto Refresh: Found element with aria-label='Home', but it cannot be clicked."); } } else { console.log("Bluesky Auto Refresh: 'Home' button with aria-label='Home' not found."); } } else { console.log("Bluesky Auto Refresh: User has scrolled down, skipping refresh."); } } } function startInterval(interval) { clearInterval(refreshIntervalId); currentInterval = interval; refreshIntervalId = setInterval(simulateHomeButtonClick, currentInterval); console.log("Bluesky Auto Refresh: Interval set to " + currentInterval + "ms"); } // Run once on page load with the default interval startInterval(currentInterval); // Listen for messages from the background script to update the interval browser.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.action === 'setInterval') { startInterval(request.interval); } }); console.log("Bluesky Auto Refresh extension is running (interval controlled by background, refresh paused when scrolled down).");