Skip to content

Commit

Permalink
Reimplement.
Browse files Browse the repository at this point in the history
- Added `start` and `stop` methods.
- Start and stop based on page load + navigation events.
  • Loading branch information
aaronfc committed Aug 21, 2023
1 parent 1a852b4 commit d89a21c
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions scripts/auto-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,56 @@

const TIMEOUT = 10000;

let timeoutId = null;

// React
const react = () => {
const eyes = document.querySelector('button[data-reaction-content="eyes"]');
if (eyes) {
if (eyes.ariaChecked !== 'true') {
const promptForReaction = () => {
const eyesButton = document.querySelector('button[data-reaction-content="eyes"]');
if (eyesButton) {
if (eyesButton.ariaChecked !== 'true') {
if (confirm("React with 👀 to this PR?")) {
eyes.click();
eyesButton.click();
}
}
} else {
console.log('Could not find the reaction button. Make sure your extension is updated!');
}
};

// Trigger react after some time.
let timeoutId = setTimeout(react, TIMEOUT);

const isMainPrUrl = (url) => url.match(/https:\/\/github.com\/.*\/pull\/\d+$/);

// Handle user navigating out and in of the main PR page.
navigation.addEventListener("navigate", e => {
const user_login = document.querySelector('meta[name="user-login"]').content;
const author_login = document.querySelector('meta[name="og:author:username"]').content;
// Start reaction countdown process.
const start = () => {
const user_login = document.querySelector('meta[name="user-login"]')?.content;
const author_login = document.querySelector('meta[property="og:author:username"]')?.content;

if (user_login === author_login) {
// Don't react to your own PRs.
return;
}

// If user navigates outside of main PR page, stop the timeout.
if (!isMainPrUrl(e.destination.url)) {
clearTimeout(timeoutId);
}
clearTimeout(timeoutId); // Just in case it's already running.
timeoutId = setTimeout(promptForReaction, TIMEOUT);
}

// Stop reaction countdown process.
const stop = () => {
clearTimeout(timeoutId);
}

// Handle user navigating out and in of the main PR page.
navigation.addEventListener("navigate", e => {
// If user navigates back to main PR page, restart the timeout.
if (isMainPrUrl(e.destination.url)) {
clearTimeout(timeoutId); // Just in case it's already running.
timeoutId = setTimeout(react, TIMEOUT);
start();
}
// If user navigates outside of main PR page, stop the timeout.
if (!isMainPrUrl(e.destination.url)) {
stop();
}
});

// Start process if already on main PR page.
if (isMainPrUrl(window.location.href)) {
start();
}

0 comments on commit d89a21c

Please sign in to comment.