drmg17928 2016-05-20 04:15
浏览 65
已采纳

从chrome-browser发送消息到ubuntu中的bash脚本来控制键盘指示灯

Is it possible for a HTML5/JavaScript web page in google-chrome on Ubuntu 16 to control keyboard LEDs?

I have a NUC5PCYH with a custom keyboard that accepts CapsLock, ScrollLock and NumLock signals as functional action inputs and a web application to control it.

I have failed to get two different test examples of Google Chrome's Native Messaging extension to work simply because I do not understand half the instructions.

Several PHP examples indicate running XSET from a PHP triggered BASH script is possible but I have zero experience with PHP and limited BASH knowledge.

Using SQLite3 to read chrome's local-storage 'variables' seemed easy enough for me to do but I can't seem to work out how to script SQLite and none of the files in google-chrome's cache are recognized as a valid database.

If I knew any Linux programming languages I'd write one to monitor the top line on the chrome browser window's client area and activate the LEDs according to binary patterns in black and near black pixels.

Can anyone point me to zero-knowledge installations of possible solutions in PHP, SQLite or Native Message Extensions?

Alternatively, where is the best place to hire a programmer for a solution?

  • 写回答

1条回答 默认 最新

  • dsc71976 2016-05-25 04:33
    关注

    I have managed to solve the problem myself using the second idea, PHP.

    Install Apache2 and PHP to set up a local host

    sudo apt-get update
    sudo apt-get install apache2
    sudo apt-get install php libapache2-mod-php

    Remove /var/www/html/index.html

    Create two files in /var/www/html/

    index.php

    <?php
    $var=$_GET['data'];
    exec("sh /var/www/html/simple.sh $var", $output);
    echo var_dump($output);
    ?>
    

    simple.sh

    #!bin/bash
    sudo echo $1 > file.txt
    echo $1
    

    Because the username for apache is www-data:

    php -r "echo exec('whoami');"

    the folder needs ownership change to apache

    sudo chown -R www-data:www-data /var/www/html

    One last file to create, on the desktop for now, but can go anywhere:

    monitor.sh

    data=`cat /var/www/html/file.txt`
    if [ $data -eq 1 ]
     then
      xset led named "Caps Lock"
     else
      xset -led named "Caps Lock"
    fi
    

    Edit some settings and permissions:

    sudo visudo

    Add the following to the end of the file:

    www-data ALL=NOPASSWD: ALL
    

    Enable changing of the CapsLock LED:

    sudo gedit /usr/share/X11/xkb/compat/ledcaps

    replace: !allowExplicit; with: allowExplicit;


    The final sequence:

    Open a browser and enter the URL:

    http://localhost/index.php?data=1
    

    index.php will get the value of data from the URL and execute simple.sh with that value as the first argument.

    simple.sh uses echo to write the argument value in $1 to the text file named file.txt.

    sh monitor.sh

    The value stored in file.txt is dumped into the variable data by the shell command cat.

    If the value in the variable data is 1 then CapsLock is turned on, and turned off if the value is anything else.

    Saving the data value into file.txt and reading from a different location using monitor.sh is due to xset error in /var/log/apache2/error.log:

    xset: unable to open display ""

    My best guess is that xset requires a display no matter what, and being launched from a shell file by PHP is without a default display. The documentation of X and xset for -display confused me so much that I used this approach after trying various suggestions. The accidental benefit is that I can host the monitor.sh file on a networked NAS box rather than locking inside /var/www/html/

    ps: Forgive me if I have missed something or forgotten some explicit detail as I am very new to these tools in this environment.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?