diff --git a/build.gradle.kts b/build.gradle.kts index 5edc2bc..8cb5140 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -32,7 +32,7 @@ kotlin { implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-js:1.6.2") implementation("com.github.ajalt.colormath:colormath-js:3.3.3") - implementation("com.jeffpdavidson.kotwords:kotwords-js:1.4.0") + implementation("com.jeffpdavidson.kotwords:kotwords-js:1.4.2") // TODO: Migrate to kotlinx-datetime if parsing/formatting support is added. implementation("com.soywiz.korlibs.klock:klock-js:4.0.10") diff --git a/src/jsMain/kotlin/com/jeffpdavidson/crosswordscraper/CrosswordScraper.kt b/src/jsMain/kotlin/com/jeffpdavidson/crosswordscraper/CrosswordScraper.kt index 9021340..d1031f3 100644 --- a/src/jsMain/kotlin/com/jeffpdavidson/crosswordscraper/CrosswordScraper.kt +++ b/src/jsMain/kotlin/com/jeffpdavidson/crosswordscraper/CrosswordScraper.kt @@ -7,6 +7,7 @@ import com.jeffpdavidson.crosswordscraper.sources.BostonGlobeSource import com.jeffpdavidson.crosswordscraper.sources.CrosshareSource import com.jeffpdavidson.crosswordscraper.sources.CrosswordCompilerSource import com.jeffpdavidson.crosswordscraper.sources.CrosswordNexusSource +import com.jeffpdavidson.crosswordscraper.sources.GuardianSource import com.jeffpdavidson.crosswordscraper.sources.NewYorkTimesSource import com.jeffpdavidson.crosswordscraper.sources.PuzzleLinkSource import com.jeffpdavidson.crosswordscraper.sources.PuzzleSocietySource @@ -69,6 +70,7 @@ object CrosswordScraper { CrosshareSource, CrosswordCompilerSource, CrosswordNexusSource, + GuardianSource, NewYorkTimesSource, PuzzleSocietySource, PzzlSource, diff --git a/src/jsMain/kotlin/com/jeffpdavidson/crosswordscraper/sources/GuardianSource.kt b/src/jsMain/kotlin/com/jeffpdavidson/crosswordscraper/sources/GuardianSource.kt new file mode 100644 index 0000000..f9ac246 --- /dev/null +++ b/src/jsMain/kotlin/com/jeffpdavidson/crosswordscraper/sources/GuardianSource.kt @@ -0,0 +1,30 @@ +package com.jeffpdavidson.crosswordscraper.sources + +import com.jeffpdavidson.crosswordscraper.Scraping +import com.jeffpdavidson.crosswordscraper.sources.Source.Companion.hostIsDomainOrSubdomainOf +import com.jeffpdavidson.kotwords.formats.Guardian +import org.w3c.dom.url.URL + +object GuardianSource : FixedHostSource() { + + override val sourceName: String = "The Guardian" + override fun neededHostPermissions(url: URL) = listOf("https://*.theguardian.com/*") + + override fun matchesUrl(url: URL): Boolean = url.hostIsDomainOrSubdomainOf("theguardian.com") + override suspend fun scrapePuzzlesWithPermissionGranted(url: URL, tabId: Int, frameId: Int): ScrapeResult { + val scrapeFn = js( + """function() { + var crosswordElems = document.getElementsByClassName("js-crossword"); + if (crosswordElems.length == 0 || !crosswordElems[0].dataset["crosswordData"]) { + return ''; + } + return crosswordElems[0].dataset["crosswordData"]; + }""" + ) + val puzzleJson = Scraping.executeFunctionForString(tabId, frameId, scrapeFn) + if (puzzleJson.isNotEmpty()) { + return ScrapeResult.Success(listOf(Guardian(puzzleJson))) + } + return ScrapeResult.Success(listOf()) + } +} \ No newline at end of file