Created
December 2, 2020 23:33
-
-
Save gander/f63a191dabd8ab6e44bba179e3444bb7 to your computer and use it in GitHub Desktop.
Use playwright to enable release notifications in all starred repositories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {chromium as browser, ElementHandle, Page} from "playwright"; | |
import {default as PQueue} from "p-queue" | |
const username: string = 'gander' | |
const links = new Set; | |
let nextPage: string = ''; | |
(async () => { | |
const pages = new PQueue({concurrency: 7}) | |
const options = {headless: false}; | |
const context = await browser.launchPersistentContext('./userDataDir', options); | |
const page = await context.newPage(); | |
await page.goto(`https://github.com/${username}?tab=stars`); | |
await processPage(page) | |
while (nextPage = await getNextPage(page)) { | |
await page.goto(nextPage); | |
await processPage(page) | |
} | |
await page.close(); | |
const values = Array.from(links.values()); | |
let counter = values.length; | |
await pages.addAll( | |
values.map(value => async () => { | |
const page = await context.newPage() | |
await page.goto(`https://github.com${value}`); | |
await setWatchReleases(page); | |
await page.waitForTimeout(1000); | |
await page.close(); | |
console.log(`${counter--} left`) | |
}) | |
) | |
await context.close(); | |
})(); | |
const processPage = async (page: Page): Promise<void> => { | |
const repos = await page.$$('css=h3>a'); | |
await Promise.all(repos.map(async repo => processRepo(repo))) | |
} | |
const processRepo = async (repo: ElementHandle): Promise<void> => { | |
const href = await repo.getAttribute('href'); | |
links.add(href) | |
} | |
const getNextPage = async (page: Page): Promise<string | null> => { | |
try { | |
const link: ElementHandle = await page.$('//a[text()="Next"]') | |
return await link.getAttribute('href'); | |
} catch { | |
return null; | |
} | |
} | |
const setWatchReleases = async (page: Page): Promise<void> => { | |
const menuButton = await page.$('//notifications-list-subscription-form//summary') | |
await menuButton.click(); | |
await page.waitForTimeout(500); | |
const customButton = await page.$('//details-menu//button[@value="ignore"]/../following-sibling::button') | |
await customButton.click(); | |
await page.waitForTimeout(750); | |
const release = await page.$('//input[@type="checkbox"][@value="Release"]'); | |
await release.check(); | |
await page.waitForTimeout(750); | |
const applyButton = await page.$('//button[text()="Apply"]') | |
const isEnabled = !await applyButton.getAttribute('disabled') | |
if (isEnabled) { | |
await applyButton.click(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment