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

com.hubspot.chrome.devtools.client.core.css.CSS Maven / Gradle / Ivy

package com.hubspot.chrome.devtools.client.core.css;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hubspot.chrome.devtools.base.ChromeRequest;
import com.hubspot.chrome.devtools.base.ChromeSessionCore;
import com.hubspot.chrome.devtools.client.core.dom.NodeId;
import com.hubspot.chrome.devtools.client.core.page.FrameId;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * This domain exposes CSS read/write operations. All CSS objects (stylesheets, rules, and styles)
 * have an associated `id` used in subsequent operations on the related object. Each object type has
 * a specific `id` structure, and those are not interchangeable between objects of different kinds.
 * CSS objects can be loaded using the `get*ForNode()` calls (which accept a DOM node id). A client
 * can also keep track of stylesheets via the `styleSheetAdded`/`styleSheetRemoved` events and
 * subsequently load the required stylesheet contents using the `getStyleSheet[Text]()` methods.
 */
public final class CSS {
  ChromeSessionCore chromeSession;

  ObjectMapper objectMapper;

  public CSS(ChromeSessionCore chromeSession, ObjectMapper objectMapper) {
    this.chromeSession = chromeSession;
    this.objectMapper = objectMapper;
  }

  /**
   * Inserts a new rule with the given `ruleText` in a stylesheet with given `styleSheetId`, at the
   * position specified by `location`.
   *
   * @param styleSheetId  The css style sheet identifier where a new rule should be inserted.
   * @param ruleText  The text of a new rule.
   * @param location  Text position of a new rule in the target style sheet.
   */
  public CSSRule addRule(StyleSheetId styleSheetId, String ruleText, SourceRange location) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.addRule");
    chromeRequest
        .putParams("styleSheetId", styleSheetId)
        .putParams("ruleText", ruleText)
        .putParams("location", location);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Inserts a new rule with the given `ruleText` in a stylesheet with given `styleSheetId`, at the
   * position specified by `location`.
   *
   * @param styleSheetId  The css style sheet identifier where a new rule should be inserted.
   * @param ruleText  The text of a new rule.
   * @param location  Text position of a new rule in the target style sheet.
   */
  public CompletableFuture addRuleAsync(StyleSheetId styleSheetId, String ruleText,
      SourceRange location) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.addRule");
    chromeRequest
        .putParams("styleSheetId", styleSheetId)
        .putParams("ruleText", ruleText)
        .putParams("location", location);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Returns all class names from specified stylesheet.
   */
  public List collectClassNames(StyleSheetId styleSheetId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.collectClassNames");
    chromeRequest
        .putParams("styleSheetId", styleSheetId);
    return chromeSession.send(chromeRequest, new TypeReference>(){});
  }

  /**
   * Returns all class names from specified stylesheet.
   */
  public CompletableFuture> collectClassNamesAsync(StyleSheetId styleSheetId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.collectClassNames");
    chromeRequest
        .putParams("styleSheetId", styleSheetId);
    return chromeSession.sendAsync(chromeRequest, new TypeReference>(){});
  }

  /**
   * Creates a new special "via-inspector" stylesheet in the frame with given `frameId`.
   *
   * @param frameId  Identifier of the frame where "via-inspector" stylesheet should be created.
   */
  public StyleSheetId createStyleSheet(FrameId frameId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.createStyleSheet");
    chromeRequest
        .putParams("frameId", frameId);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Creates a new special "via-inspector" stylesheet in the frame with given `frameId`.
   *
   * @param frameId  Identifier of the frame where "via-inspector" stylesheet should be created.
   */
  public CompletableFuture createStyleSheetAsync(FrameId frameId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.createStyleSheet");
    chromeRequest
        .putParams("frameId", frameId);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Disables the CSS agent for the given page.
   */
  public void disable() {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.disable");
    chromeSession.send(chromeRequest);
  }

  /**
   * Disables the CSS agent for the given page.
   */
  public void disableAsync() {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.disable");
    chromeSession.sendAsync(chromeRequest);
  }

  /**
   * Enables the CSS agent for the given page. Clients should not assume that the CSS agent has been
   * enabled until the result of this command is received.
   */
  public void enable() {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.enable");
    chromeSession.send(chromeRequest);
  }

  /**
   * Enables the CSS agent for the given page. Clients should not assume that the CSS agent has been
   * enabled until the result of this command is received.
   */
  public void enableAsync() {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.enable");
    chromeSession.sendAsync(chromeRequest);
  }

  /**
   * Ensures that the given node will have specified pseudo-classes whenever its style is computed by
   * the browser.
   *
   * @param nodeId  The element id for which to force the pseudo state.
   * @param forcedPseudoClasses  Element pseudo classes to force when computing the element's style.
   */
  public void forcePseudoState(NodeId nodeId, List forcedPseudoClasses) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.forcePseudoState");
    chromeRequest
        .putParams("nodeId", nodeId)
        .putParams("forcedPseudoClasses", forcedPseudoClasses);
    chromeSession.send(chromeRequest);
  }

  /**
   * Ensures that the given node will have specified pseudo-classes whenever its style is computed by
   * the browser.
   *
   * @param nodeId  The element id for which to force the pseudo state.
   * @param forcedPseudoClasses  Element pseudo classes to force when computing the element's style.
   */
  public void forcePseudoStateAsync(NodeId nodeId, List forcedPseudoClasses) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.forcePseudoState");
    chromeRequest
        .putParams("nodeId", nodeId)
        .putParams("forcedPseudoClasses", forcedPseudoClasses);
    chromeSession.sendAsync(chromeRequest);
  }

  public GetBackgroundColorsResult getBackgroundColors(NodeId nodeId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.getBackgroundColors");
    chromeRequest
        .putParams("nodeId", nodeId);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  public CompletableFuture getBackgroundColorsAsync(NodeId nodeId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.getBackgroundColors");
    chromeRequest
        .putParams("nodeId", nodeId);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Returns the computed style for a DOM node identified by `nodeId`.
   */
  public List getComputedStyleForNode(NodeId nodeId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.getComputedStyleForNode");
    chromeRequest
        .putParams("nodeId", nodeId);
    return chromeSession.send(chromeRequest, new TypeReference>(){});
  }

  /**
   * Returns the computed style for a DOM node identified by `nodeId`.
   */
  public CompletableFuture> getComputedStyleForNodeAsync(
      NodeId nodeId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.getComputedStyleForNode");
    chromeRequest
        .putParams("nodeId", nodeId);
    return chromeSession.sendAsync(chromeRequest, new TypeReference>(){});
  }

  /**
   * Returns the styles defined inline (explicitly in the "style" attribute and implicitly, using DOM
   * attributes) for a DOM node identified by `nodeId`.
   */
  public GetInlineStylesForNodeResult getInlineStylesForNode(NodeId nodeId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.getInlineStylesForNode");
    chromeRequest
        .putParams("nodeId", nodeId);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Returns the styles defined inline (explicitly in the "style" attribute and implicitly, using DOM
   * attributes) for a DOM node identified by `nodeId`.
   */
  public CompletableFuture getInlineStylesForNodeAsync(
      NodeId nodeId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.getInlineStylesForNode");
    chromeRequest
        .putParams("nodeId", nodeId);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Returns requested styles for a DOM node identified by `nodeId`.
   */
  public GetMatchedStylesForNodeResult getMatchedStylesForNode(NodeId nodeId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.getMatchedStylesForNode");
    chromeRequest
        .putParams("nodeId", nodeId);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Returns requested styles for a DOM node identified by `nodeId`.
   */
  public CompletableFuture getMatchedStylesForNodeAsync(
      NodeId nodeId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.getMatchedStylesForNode");
    chromeRequest
        .putParams("nodeId", nodeId);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Returns all media queries parsed by the rendering engine.
   */
  public List getMediaQueries() {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.getMediaQueries");
    return chromeSession.send(chromeRequest, new TypeReference>(){});
  }

  /**
   * Returns all media queries parsed by the rendering engine.
   */
  public CompletableFuture> getMediaQueriesAsync() {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.getMediaQueries");
    return chromeSession.sendAsync(chromeRequest, new TypeReference>(){});
  }

  /**
   * Requests information about platform fonts which we used to render child TextNodes in the given
   * node.
   */
  public List getPlatformFontsForNode(NodeId nodeId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.getPlatformFontsForNode");
    chromeRequest
        .putParams("nodeId", nodeId);
    return chromeSession.send(chromeRequest, new TypeReference>(){});
  }

  /**
   * Requests information about platform fonts which we used to render child TextNodes in the given
   * node.
   */
  public CompletableFuture> getPlatformFontsForNodeAsync(NodeId nodeId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.getPlatformFontsForNode");
    chromeRequest
        .putParams("nodeId", nodeId);
    return chromeSession.sendAsync(chromeRequest, new TypeReference>(){});
  }

  /**
   * Returns the current textual content and the URL for a stylesheet.
   */
  public String getStyleSheetText(StyleSheetId styleSheetId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.getStyleSheetText");
    chromeRequest
        .putParams("styleSheetId", styleSheetId);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Returns the current textual content and the URL for a stylesheet.
   */
  public CompletableFuture getStyleSheetTextAsync(StyleSheetId styleSheetId) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.getStyleSheetText");
    chromeRequest
        .putParams("styleSheetId", styleSheetId);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Find a rule with the given active property for the given node and set the new value for this
   * property
   *
   * @param nodeId  The element id for which to set property.
   */
  public void setEffectivePropertyValueForNode(NodeId nodeId, String propertyName, String value) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.setEffectivePropertyValueForNode");
    chromeRequest
        .putParams("nodeId", nodeId)
        .putParams("propertyName", propertyName)
        .putParams("value", value);
    chromeSession.send(chromeRequest);
  }

  /**
   * Find a rule with the given active property for the given node and set the new value for this
   * property
   *
   * @param nodeId  The element id for which to set property.
   */
  public void setEffectivePropertyValueForNodeAsync(NodeId nodeId, String propertyName,
      String value) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.setEffectivePropertyValueForNode");
    chromeRequest
        .putParams("nodeId", nodeId)
        .putParams("propertyName", propertyName)
        .putParams("value", value);
    chromeSession.sendAsync(chromeRequest);
  }

  /**
   * Modifies the keyframe rule key text.
   */
  public Value setKeyframeKey(StyleSheetId styleSheetId, SourceRange range, String keyText) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.setKeyframeKey");
    chromeRequest
        .putParams("styleSheetId", styleSheetId)
        .putParams("range", range)
        .putParams("keyText", keyText);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Modifies the keyframe rule key text.
   */
  public CompletableFuture setKeyframeKeyAsync(StyleSheetId styleSheetId, SourceRange range,
      String keyText) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.setKeyframeKey");
    chromeRequest
        .putParams("styleSheetId", styleSheetId)
        .putParams("range", range)
        .putParams("keyText", keyText);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Modifies the rule selector.
   */
  public CSSMedia setMediaText(StyleSheetId styleSheetId, SourceRange range, String text) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.setMediaText");
    chromeRequest
        .putParams("styleSheetId", styleSheetId)
        .putParams("range", range)
        .putParams("text", text);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Modifies the rule selector.
   */
  public CompletableFuture setMediaTextAsync(StyleSheetId styleSheetId, SourceRange range,
      String text) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.setMediaText");
    chromeRequest
        .putParams("styleSheetId", styleSheetId)
        .putParams("range", range)
        .putParams("text", text);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Modifies the rule selector.
   */
  public SelectorList setRuleSelector(StyleSheetId styleSheetId, SourceRange range,
      String selector) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.setRuleSelector");
    chromeRequest
        .putParams("styleSheetId", styleSheetId)
        .putParams("range", range)
        .putParams("selector", selector);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Modifies the rule selector.
   */
  public CompletableFuture setRuleSelectorAsync(StyleSheetId styleSheetId,
      SourceRange range, String selector) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.setRuleSelector");
    chromeRequest
        .putParams("styleSheetId", styleSheetId)
        .putParams("range", range)
        .putParams("selector", selector);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Sets the new stylesheet text.
   */
  public String setStyleSheetText(StyleSheetId styleSheetId, String text) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.setStyleSheetText");
    chromeRequest
        .putParams("styleSheetId", styleSheetId)
        .putParams("text", text);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Sets the new stylesheet text.
   */
  public CompletableFuture setStyleSheetTextAsync(StyleSheetId styleSheetId, String text) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.setStyleSheetText");
    chromeRequest
        .putParams("styleSheetId", styleSheetId)
        .putParams("text", text);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Applies specified style edits one after another in the given order.
   */
  public List setStyleTexts(List edits) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.setStyleTexts");
    chromeRequest
        .putParams("edits", edits);
    return chromeSession.send(chromeRequest, new TypeReference>(){});
  }

  /**
   * Applies specified style edits one after another in the given order.
   */
  public CompletableFuture> setStyleTextsAsync(List edits) {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.setStyleTexts");
    chromeRequest
        .putParams("edits", edits);
    return chromeSession.sendAsync(chromeRequest, new TypeReference>(){});
  }

  /**
   * Enables the selector recording.
   */
  public void startRuleUsageTracking() {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.startRuleUsageTracking");
    chromeSession.send(chromeRequest);
  }

  /**
   * Enables the selector recording.
   */
  public void startRuleUsageTrackingAsync() {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.startRuleUsageTracking");
    chromeSession.sendAsync(chromeRequest);
  }

  /**
   * Stop tracking rule usage and return the list of rules that were used since last call to
   * `takeCoverageDelta` (or since start of coverage instrumentation)
   */
  public List stopRuleUsageTracking() {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.stopRuleUsageTracking");
    return chromeSession.send(chromeRequest, new TypeReference>(){});
  }

  /**
   * Stop tracking rule usage and return the list of rules that were used since last call to
   * `takeCoverageDelta` (or since start of coverage instrumentation)
   */
  public CompletableFuture> stopRuleUsageTrackingAsync() {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.stopRuleUsageTracking");
    return chromeSession.sendAsync(chromeRequest, new TypeReference>(){});
  }

  /**
   * Obtain list of rules that became used since last call to this method (or since start of coverage
   * instrumentation)
   */
  public List takeCoverageDelta() {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.takeCoverageDelta");
    return chromeSession.send(chromeRequest, new TypeReference>(){});
  }

  /**
   * Obtain list of rules that became used since last call to this method (or since start of coverage
   * instrumentation)
   */
  public CompletableFuture> takeCoverageDeltaAsync() {
    ChromeRequest chromeRequest = new ChromeRequest("CSS.takeCoverageDelta");
    return chromeSession.sendAsync(chromeRequest, new TypeReference>(){});
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy