填写“隐藏”表单并点击“隐藏”按钮

时间:2015-12-07 18:55:42

标签: javascript jquery applescript greasemonkey

this site上,我正在尝试自动执行以下过程:

  1. 点击上传
  2. 点击“添加图片网址”
  3. 输入网址
  4. 点击提交。
  5. 在此之前,脚本从亚马逊页面获取图像URL,我能够做到这一点。它只是上面的步骤,我迷失了。

    我已经尝试了一个我发现的名为“waitforkeyelements”的功能,以及我发现的其他类似的东西,但无济于事。我似乎甚至无法点击最初的“上传”按钮,但我注意到网址没有改变(至少在步骤1-3中没有),表格和按钮的html总是在那里,所以我认为它仍然可填写的(只是“隐藏”)。我以为我已经弄明白了,但显然不是。有人可以提供任何见解吗?在这里我到目前为止:(它的意思是从像this one这样的页面开始)

    // ==UserScript==
    // @name        Test
    // @version     0.1
    // @description testing javascript stuff
    // @match       http://www.amazon.com/*
    // @match       https://www.amazon.com/*
    // @require     http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
    // @require  https://gist.github.com/raw/2625891/waitForKeyElements.js// 
    // @grant       none
    // ==/UserScript==
    /*- The @grant directive is needed to work around a design change
        introduced in GM 1.0.   It restores the sandbox.
    */
    //-- If/when SSL becomes available, switch to only using the https pages.
    
    //$("#artist").val ("username_here");
    
    location.href='http://www.example.com';
    
      $('body').append('<input type="button" value="Open Image" id="CP">')
      $("#CP").css("position", "fixed").css("bottom", 0).css("right", 0);
      $('#CP').click(function(){ 
          var coverimageurl = $(imgBlkFront).get(0).src;
          //location.href=coverimageurl
          location.href='https://upload.vstanced.com';
    
          $(".resize-vertical").val (coverimageurl);
          //$("#fullscreen-modal-body").val (coverimageurl);
    
          $('#btn btn-input default').click()
      });
    

    id也有兴趣使用javascript将图片“拖放”到该页面上。

    还有一种更简单的方法来测试一行或两行javascript,而不是重复编辑脚本,保存它并刷新@match页面吗?

    编辑:继承人我现在正在做什么,它的AppleScript有点奇怪,但它确实有效:

    set filepath to POSIX path of theimage
    
        tell application "Safari" to open location "https://upload.vstanced.com"
        delay 2
        clickClassName("btn btn-big white outline", 0)
        tell application "System Events"
            activate application "Safari"
            delay 0.5
            keystroke "g" using {shift down, command down} --open goto
            set the clipboard to filepath
            keystroke "v" using {command down}
            delay 0.7
            keystroke return -- enter goto text
            delay 0.4
            keystroke return --press enter on file
    
        end tell
    

    它是一个小滴,所以当我在其上放一个文件时它会运行并获得“theimage”。我猜这不相关。什么是重要的是我得到了很多javascript与applecript玩得很好,就像clickclassname事情使用javascript:

    to clickClassName(theClassName, elementnum)
        tell application "Safari"
            do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
        end tell
    end clickClassName
    

    但是我试图制作一个功能来完成你的第3步和第4步的版本并且它没有用,我还有一点使用函数来填写我在网上找到的表格:

    to inputByName(theName, num, theValue)
        tell application "Safari"
            do JavaScript "\n  document.getElementsByName('" & theName & "')[" & num & "].value ='" & theValue & "';" in document 1
        end tell
    end inputByName
    
    inputByName("urls", 0, "/Users/Paul/Desktop/image.jpg")
    

    我可以看到这已经填写了url表单,并且我为整个.submit()部分创建了自己的函数,但是它没有做它应该做的事情。当我运行这个:

    tell application "Safari"
        do JavaScript "document.forms[0].submit();" in document 1
    end tell
    

    我得到其中一个“你确定要离开页面吗?你写的东西会丢失”的消息。如果我点击否,没有任何反应(我假设因为它没有做“提交”的事情)。如果我离开页面,我会回到原点。然而,有趣的事情发生了,网址就会变成现在的结尾“/?urls=%2FUsers%2FPaul%2FDesktop%2Fimage.jpg”

    我还找到了一个点击功能,并试过了,但出于某种原因我运行时:

    to clickClassName(theClassName, elementnum)
        tell application "Safari"
            do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
        end tell
    end clickClassName --^credits to cubemg for this
    
    clickClassName("btn btn-input default", 0)
    

    它关闭了小网址框。 (即使它里面有东西,当我点击提交按钮上的inspect元素时出现的类名。)

    我已经尝试过创建一个帐户并进行操作,但只是为了将来的任何尝试,您在运行时是否已登录?

    我将继续修补它,不知道为什么我甚至把这个放在这个以这个以javascript为中心的标题的帖子中。我已经采用了一种方法,它使用了更多可行的AppleScript,但它有点奇怪,它使用了大量的延迟和击键。

1 个答案:

答案 0 :(得分:0)

从VSstance页面,我认为这很容易:

//Step 3 - add data to the form
var url = "/path/to/my/photos/photo.jpg";
document.getElementsByName('urls')[0].value = url;
//Step 4 - submit the form
document.forms[0].submit();

但是,我认为如果我理解正确,那么您希望在amazon.com上使用来自amazon.com的数据进行此操作。在这种情况下,您可能需要查看ajax / post。我认为location.href修改将停止执行这个脚本,也许这就是你被困的地方?这不起作用,但我的意思类似于:

$.post('https://upload.vstanced.com/', { 
    urls: "http://www.amazon.com/picture.jpg"
    });

我看到的另一个选择是将upload.vstanced.com通过iframe集成到amazon.com。

相关问题