All Downloads are FREE. Search and download functionalities are using the official Maven repository.

coursier.util.WebPageCompatibility.scala Maven / Gradle / Ivy

There is a newer version: 2.1.18
Show newest version
package coursier.util

import scala.collection.mutable.ListBuffer
import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => g}

private[coursier] abstract class WebPageCompatibility {

  // FIXME Won't work in the browser
  lazy val cheerio = g.require("cheerio")

  lazy val jqueryAvailable = js.typeOf(g.$) != "undefined"

  def listWebPageRawElements(page: String): Iterator[String] = {

    val links = new ListBuffer[String]

    // getting weird "maybe a wrong Dynamic method signature" errors when trying to factor that more

    if (jqueryAvailable)
      g.$("
").html(page).find("a").each({ self: js.Dynamic => val href = g.$(self).attr("href") if (js.typeOf(href) != "undefined") links += href.asInstanceOf[String] () }: js.ThisFunction0[js.Dynamic, Unit]) else { val jquery = cheerio.load(page) jquery("a").each({ self: js.Dynamic => val href = jquery(self).attr("href") if (js.typeOf(href) != "undefined") links += href.asInstanceOf[String] () }: js.ThisFunction0[js.Dynamic, Unit]) } links.result().iterator } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy