Skip to content

Instantly share code, notes, and snippets.

@Zirak
Created June 6, 2015 19:01
Show Gist options
  • Select an option

  • Save Zirak/4d10b35b420ea469eaed to your computer and use it in GitHub Desktop.

Select an option

Save Zirak/4d10b35b420ea469eaed to your computer and use it in GitHub Desktop.

Make $$ return an array

Right now $$ returns a static (non-live) NodeList, but we want to use array functions on it. Let’s make it work.

Find where console functions live

third_party/WebKit/Source/core/inspector/InjectedScriptSource.js

Wrap the call

var res;
if (this._canQuerySelectorOnNode(opt_startNode))
    res = opt_startNode.querySelectorAll(selector);
else
    res = inspectedWindow.document.querySelectorAll(selector);

return slice(res);

Textarea’s textContent

Wouldn’t it be fun if we could do `textarea.textContent` and it’d return the value? And have a setter as well?

Find where a textarea is defined

third_party/WebKit/Source/core/html

Find the value getter/setter

Add a textContent hook

// HTMLTextAreaElement.idl
[TreatNullAs=NullString] attribute DOMString textContent;

// HTMLTextAreaElement.h
virtual String textContent() const;
void setTextContent(const String&, TextFieldEventBehavior = DispatchNoEvent);

// HTMLTextAreaElement.cpp
String HTMLTextAreaElement::textContent() const
{
    return value();
}
void HTMLTextAreaElement::setTextContent(const String& value, TextFieldEventBehavior eventBehavior) {
    return setValue(value, eventBehavior);
}

Maybe make it fancier?

Array..includes

ES7 defines Array..include, but it’s not in v8 yet. Let’s implement it!

Find where Arrays are mplemented

v8/src/array.js

Write shit!

function ArrayIncludes(element) {
  CHECK_OBJECT_COERCIBLE(this, "Array.prototype.includes");

  var length = TO_UINT32(this.length);
  if (length == 0) return false;

  var start = 0;
  if (%_ArgumentsLength() > 1) {
    start = %_Arguments(1);
    start = ToInteger(start);

    if (start < 0) {
      start = $max(length + start, 0);
    }
  }

  if (start >= length) {
    return false;
  }

  for (var i = start; i < length; i += 1) {
    if (SameValueZero(this[i], element)) {
      return true;
    }
  }

  return false;
}

Test some edge cases

Make it smooth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment