1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[js] screen.availWidth (box.width * 8)

This commit is contained in:
Witold Filipczyk 2021-05-29 13:42:34 +02:00
parent 10d731f10c
commit 719f484422
2 changed files with 58 additions and 0 deletions

View File

@ -58,9 +58,11 @@ JSClass screen_class = {
};
static bool screen_get_property_availHeight(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool screen_get_property_availWidth(JSContext *ctx, unsigned int argc, JS::Value *vp);
JSPropertySpec screen_props[] = {
JS_PSG("availHeight", screen_get_property_availHeight, JSPROP_ENUMERATE),
JS_PSG("availWidth", screen_get_property_availWidth, JSPROP_ENUMERATE),
JS_PS_END
};
@ -100,3 +102,40 @@ screen_get_property_availHeight(JSContext *ctx, unsigned int argc, JS::Value *vp
return true;
}
static bool
screen_get_property_availWidth(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct view_state *vs;
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, hobj, &screen_class, NULL))
return false;
vs = interpreter->vs;
if (!vs) {
return false;
}
struct document_view *doc_view = vs->doc_view;
if (!doc_view) {
return false;
}
args.rval().setInt32(doc_view->box.width * 8);
return true;
}

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display the avail width of your screen, in pixels.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = "Avail Width: " + screen.availWidth + "px";
alert(x);
}
</script>
</body>
</html>