Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add content_script manifest support #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ lazy val extension = project.in(file("extension"))
val background = Background(
scripts = Chrome.defaultScripts
)
override val contentScripts = List(
ContentScript(
matches = Set("https://www.example.org/*"),
js = Chrome.defaultScripts
)
)
val name = Keys.name.value
val version = Keys.version.value
override val icons = Chrome.icons(
Expand Down
1 change: 1 addition & 0 deletions examples/extension/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import scala.scalajs.js
import scala.scalajs.js.JSApp

object Main extends JSApp {
Expand Down
21 changes: 21 additions & 0 deletions sbt-plugin/src/main/scala/net/lullabyte/JsonCodecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ object JsonCodecs {
)
}

implicit val runAtEncoder = Encoder.instance[chrome.RunAt] {
case RunAt.DocumentStart => Json.fromString("document_start")
case RunAt.DocumentEnd => Json.fromString("document_end")
case RunAt.DocumentIdle => Json.fromString("document_idle")
}

implicit val contentScriptEncoder = Encoder.instance[chrome.ContentScript] { cs =>
Json.obj(
("matches", Json.fromValues(cs.matches.map(Json.fromString))),
("exclude_matches", omitIfEmpty(cs.excludeMatches)(Json.fromString)),
("match_about_blank", cs.matchAboutBlank.asJson),
("css", omitIfEmpty(cs.css)(Json.fromString)),
("js", omitIfEmpty(cs.js)(Json.fromString)),
("run_at", cs.runAt.asJson),
("all_frames", cs.allFrames.asJson),
("include_globs", omitIfEmpty(cs.includeGlobs)(Json.fromString)),
("exclude_globs", omitIfEmpty(cs.css)(Json.fromString))
)
}

implicit val omniboxEncoder = Encoder.instance[chrome.Omnibox] { omnibox =>
Json.obj(
("keyword", Json.fromString(omnibox.keyword))
Expand Down Expand Up @@ -187,6 +207,7 @@ object JsonCodecs {
val commonValues = manifest2json(manifest)
val extValues = Seq(
("background", manifest.background.asJson),
("content_scripts", omitIfEmpty(manifest.contentScripts)(_.asJson)),
("omnibox", manifest.omnibox.asJson),
("options_ui", manifest.optionsUI.asJson),
("browser_action", manifest.browserAction.asJson),
Expand Down
19 changes: 19 additions & 0 deletions shared/src/main/scala/chrome/Manifest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ sealed trait Manifest {

case class Background(scripts: List[String])
case class App(background: Background)
case class ContentScript(
matches: Set[String],
excludeMatches: Set[String] = Set.empty,
matchAboutBlank: Option[Boolean] = None,
css: List[String] = List.empty,
js: List[String] = List.empty,
runAt: Option[RunAt] = None,
allFrames: Option[Boolean] = None,
includeGlobs: Set[String] = Set.empty,
excludeGlobs: Set[String] = Set.empty
)

sealed trait RunAt
object RunAt {
case object DocumentStart extends RunAt
case object DocumentEnd extends RunAt
case object DocumentIdle extends RunAt
}

case class BrowserAction(
icon: Map[Int, String] = Map.empty,
Expand Down Expand Up @@ -98,6 +116,7 @@ trait AppManifest extends chrome.Manifest {

trait ExtensionManifest extends chrome.Manifest {
val background: Background
val contentScripts: List[ContentScript] = List.empty
val browserAction: Option[BrowserAction] = None
val omnibox: Option[Omnibox] = None
val optionsUI: Option[OptionsUI] = None
Expand Down