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

[js] screen.width

This commit is contained in:
Witold Filipczyk 2021-05-30 12:58:19 +02:00
parent 719f484422
commit 54a7bd7339
2 changed files with 65 additions and 0 deletions

View File

@ -59,10 +59,12 @@ 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);
static bool screen_get_property_width(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_PSG("width", screen_get_property_width, JSPROP_ENUMERATE),
JS_PS_END
};
@ -139,3 +141,47 @@ screen_get_property_availWidth(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool
screen_get_property_width(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;
}
struct session *ses = doc_view->session;
if (!ses) {
return false;
}
args.rval().setInt32(ses->tab->term->width * 8);
return true;
}

View File

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