简体   繁体   English

点击<select>和</select><input>页面加载后

[英]Click <select> and <input> after page load

I am writing a Greasemonkey script to interact with orders on Shipstation.com.我正在编写一个 Greasemonkey 脚本来与 Shipstation.com 上的订单进行交互。

The script will select certain values in <select> and <input> elements of the order modal based on certain criteria.该脚本将 select 根据特定条件在顺序模式的<select><input>元素中确定某些值。

Thus, the script must be able to interact with these elements.因此,脚本必须能够与这些元素交互。

However, I cannot figure out how to do so.但是,我不知道该怎么做。

So far, I have tried to do the following and have been unable to trigger a click on the element:到目前为止,我已尝试执行以下操作,但无法触发对该元素的点击:

  • Set the value of the element using JS .value使用 JS .value设置元素的值
  • Set the value of the element using jQuery .val使用 jQuery .val设置元素的值
  • Trigger a click event on the element using JS .click()使用 JS .click()在元素上触发点击事件
  • Trigger a click event on the element using this code:使用此代码在元素上触发点击事件:
function triggerMostButtons (jNode) {
    triggerMouseEvent (jNode, "mouseover");
    triggerMouseEvent (jNode, "mousedown");
    triggerMouseEvent (jNode, "mouseup");
    triggerMouseEvent (jNode, "click");
}

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}

triggerMostButtons(jNode);

It appears that Shipstation is locking the value of <select> and <input> values.看起来 Shipstation 正在锁定<select><input>值的值。

Here are examples of the SO questions I have read to try and figure this out.以下是我阅读过的 SO 问题的示例,以尝试解决这个问题。 I haven't been able to trigger a click on these elements using any of these approaches:我无法使用以下任何一种方法触发对这些元素的点击:

How else can I trigger a click on these elements?我还能如何触发对这些元素的点击?

Alternatively, how can I set the value of these fields using JS?或者,如何使用 JS 设置这些字段的值? Do I have to find the data model in the JS and edit the value directly?是不是一定要在JS中找到数据model直接编辑值? Or find a way to hijack the functions that are triggered when a user clicks on these elements?或者找到一种方法来劫持用户单击这些元素时触发的功能?

在此处输入图像描述

Maybe the values are loaded dynamically from the server, so you need to wait until the full page is loaded by adding a load event .也许这些值是从服务器动态加载的,因此您需要通过添加load event等到整个页面加载完毕。

Then grab the select elements by a document.getElementById (or querySelector ) and set your desired value (If I understood correctly, you don't need a click event).然后通过document.getElementById (或querySelector )获取select元素并设置您想要的值(如果我理解正确的话,您不需要点击事件)。

You didn't provide an example I can work with, but I tried it with this https://www.shipstation.com/step1/ Notice how after the page loads, the country will be set to Canada:您没有提供我可以使用的示例,但我尝试使用此https://www.shipstation.com/step1/注意页面加载后,国家/地区将如何设置为加拿大:

// ==UserScript==
// @name     Unnamed Script 933923
// @version  1
// @grant    none
// @match *://*.shipstation.com/*
// ==/UserScript==


window.addEventListener('load', (event) => {
  
  document.getElementById('country').value = "CA";
  document.getElementById('howdidyouhearaboutus').value = "Banner Ad";
  
});

you can select an option by doing this你可以通过这样做 select 一个选项

function selectItemInDropdownList(selectElement, ov) {
  const optionToSelect = '' + ov
  const options = selectElement.getElementsByTagName('option')
  for (const optionEle of options) {
    if (optionToSelect === optionEle.innerText || optionToSelect === optionEle.value) {
      optionEle.selected = true // selects this option
      return true
    }
  }
  return false // failed
}

/*
const dropdownEle = document.getElementById('whatever')
const status = selectItemInDropdownList(dropdownEle, "WHAT") 
console.log(status ? 'success' : 'failed')
*/

I think main issue is its a popup form and the select element 'may' not be available in DOM tree at first.我认为主要问题是它的弹出窗体和select元素“可能”最初在 DOM 树中不可用。 so make sure to call this function when its available.因此,请务必在可用时拨打此电话 function。

This solution works well in 2022. No jQuery is used.该方案在2022年运行良好。没有使用jQuery。

Auto expand select options in element cb :在元素cb中自动扩展 select 选项:

cb.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }))

Input focus on element cb (tested on Safari), watch the compatibility note on MDN :在元素cb上输入焦点(在 Safari 上测试),查看MDN上的兼容性说明:

cb.focus()

Below are the working solution for you to copy and run on a browser.以下是供您复制并在浏览器上运行的工作解决方案。 (Since input focus feature is failed to run in the code snippet here, while the feature auto expand the select options still works.) (由于输入焦点功能在此处的代码片段中运行失败,而自动扩展 select 选项的功能仍然有效。)

 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <title>Document</title> </head> <body> <select id="select-1"> <option value="1">Option 1</option> <option value="2">Option 2</option> </select> <input id="input-1" type="text"/> <script> window.onload = function() { const select = document;getElementById('select-1'). const input = document;getElementById('input-1'). input;focus(). setTimeout(function() { select,dispatchEvent(new MouseEvent('mousedown': { bubbles; true })), }; 1000); } </script> </body> </html>

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

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