【问题标题】:Tampermonkey script only sporadically working?Tampermonkey 脚本只能偶尔工作?
【发布时间】:2017-12-05 06:26:28
【问题描述】:

我正在尝试让 Tampermonkey 填写在线表格。 它每 4 次就有 1 次有效,我想要它做的只是在 bigcartel 商店进行简单的结帐过程。有人可以帮忙吗?

它应该适用于使用他们平台的任何商店,因为它们都很通用,即http://groundup.bigcartel.com

我的代码;

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @include      https://checkout.bigcartel.com/*
// @include      https://*.bigcartel.com/product
// @include      https://*.bigcartel.com/cart
// @grant        none
// ==/UserScript==

// on "/cart" page click checkout button 
document.getElementByName("checkout").click();

// fill first three form fields
document.getElementById("buyer_first_name").value = "John";
document.getElementById("buyer_last_name").value = "Smith";
document.getElementById("buyer_email").value = "john@doe.com";

// click "next" button
document.getElementByType("submit").click();

【问题讨论】:

    标签: javascript tampermonkey


    【解决方案1】:

    您的 TM 脚本存在四个主要问题。

    1.) 您的包含标签使用https 而不是http

    2.) document.getElementByName 不存在。

    修复:使用document.getElementsByName("checkout")[0]

    3.) 点击checkout按钮后,脚本会立即尝试设置输入字段的值,您必须等待页面加载。

    4.) document.getElementByType 也不存在。

    这是工作脚本:

    // ==UserScript==
    // @name         Script
    // @version      0.1
    // @description  try to take over the world!
    // @author       You
    // @include      https://checkout.bigcartel.com/*
    // @include      http://*.bigcartel.com/product
    // @include      http://*.bigcartel.com/cart
    // @grant        none
    // ==/UserScript==
    
    // on "/cart" page click checkout button
    if (window.location.origin !== "https://checkout.bigcartel.com") document.getElementsByName("checkout")[0].click();
    else {
        // fill first three form fields
        document.getElementById("buyer_first_name").value = "John";
        document.getElementById("buyer_last_name").value = "Smith";
        document.getElementById("buyer_email").value = "john@doe.com";
        // click "next" button
        document.getElementsByTagName("button")[0].click();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 2014-01-08
      • 2020-11-10
      相关资源
      最近更新 更多