-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See #46
- Loading branch information
Showing
3 changed files
with
33 additions
and
1 deletion.
There are no files selected for viewing
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
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
30 changes: 30 additions & 0 deletions
30
src/jsMain/kotlin/com/jeffpdavidson/crosswordscraper/sources/GuardianSource.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} | ||
} |