简体   繁体   English

从命令行打开Chrome / Chromium,然后将javascript操作发送到chrome / chromium控制台

[英]Open Chrome/Chromium from command line and send javascript action to chrome/chromium console

On Ubuntu 18.04 , I'm trying to create an alert to remember me to track my worklog everyday. Ubuntu 18.04上 ,我试图创建一个警报以记住我每天跟踪我的工作日志。

Create sh file put_hours.sh 创建sh文件put_hours.sh

#!/bin/bash

zenity \
    --forms \
    --title="TIME TRACKING" \
    --text="Please track your time" \
    --add-entry="¿ What have you done ?" \
    --ok-label "GO"

case $? in
    0) chromium-browser http://site/that/I/want/to/open
    ;;
    1)
   ;;
esac

Make it executable 使它可执行

chmod +x put_hours.sh

Run it 运行

./put_hours.sh

zenity创建的窗口

As you can see this works but my question is: 如您所见,这可行,但是我的问题是:

How can I send a javascript command to the chrome/chromium console in the opened url? 如何在打开的网址中向chrome / chromium控制台发送javascript命令?

I need to run a simple command like: 我需要运行一个简单的命令,例如:

document.getElementById("my_id").value = 'nothing to do'

the value to send as string 'nothing to do' should be the value passed by the window opened by Zenity. 作为字符串“无事可做”发送的值应该是Zenity打开的窗口传递的值。

I use Zenity to make the window but I'm not forced to use this software. 我使用Zenity制作窗口,但我没有被迫使用此软件。

First, Get the value 首先,获取价值

Zenity will print the amount of hours the user entered to stdout after you click GO . 单击GO后,Zenity将打印用户输入到标准输出的小时数。

The first step would be to modify your script to capture the result and assign it to a variable: 第一步是修改脚本以捕获结果并将其分配给变量:

hours=$(zenity \
    --forms \
    --title="TIME TRACKING" \
    --text="Please track your time" \
    --add-entry="¿ What have you done ?" \
    --ok-label "GO")

Options 选项

What you can do next depends on the type of access and control you have over the website. 接下来的操作取决于对网站的访问和控制类型。 Some options are hacks. 一些选择是黑客。

For each of the following options pass the $hours variable to the website as a query param, eg: 对于以下每个选项,将$hours变量作为查询参数传递给网站,例如:

case $? in
    0) chromium-browser "http://site/that/I/want/to/open?hours=$hours"

Option A - Website supports populating the field by query param 选项A-网站支持按查询参数填充字段

You may get lucky and the above will just work. 您可能会很幸运,上面的方法将起作用。 If it doesn't work first go you may have the wrong query param name. 如果首先不起作用,则您可能输入了错误的查询参数名称。

To determine what the correct query param name is, examine the source code and how it behaves when you submit the form manually. 要确定正确的查询参数名称是什么,请检查源代码以及手动提交表单时的行为。

Eg https://www.google.com?q=pre+populate+the+search+bar 例如https://www.google.com?q=pre+populate+the+search+bar

Option B - Write a user script to do it in the browser 选项B-编写用户脚本以在浏览器中进行操作

Use the Tamper Monkey tool to run a user script when the page loads, that pulls the query param and sets the field. 在页面加载时,使用篡改猴子工具运行用户脚本,该脚本可提取查询参数并设置字段。

// ==UserScript==
// @name         Set Hours
// @match        http://site/that/I/want/to/open*
// ==/UserScript==
(function() {
    var urlParams = new URLSearchParams(window.location.search); // Reference: https://stackoverflow.com/a/47566165/241294

    if (urlParams.has('hours')) {
        var hours = urlParams.get('hours');

        document.getElementById("my_id").value = hours;

        alert('well done ad3luc you did ' + hours + ' hours today');
    }
})();

A downside of course is now the logic is in two places, your script and in your the browser. 当然,缺点是逻辑在两个地方,脚本和浏览器中。

It is a hack, but I do like the separation of concerns. 这是一个hack,但我确实喜欢分离关注点。 The script sends the value to the website. 该脚本将值发送到网站。 The website, sets the field value. 该网站设置字段值。

You would also need to ensure the param you choose doesn't clash with any of the existing query params the site uses. 您还需要确保选择的参数不会与网站使用的任何现有查询参数冲突。

Option C - Modify the website 选项C-修改网站

Change the website's source code so that it supports Option A. 更改网站的源代码,使其支持选项A。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM