12

我正在尝试创建一个小书签来解析页面并通过我定义的表单将结果发送到 googledocs 电子表格。

脚本的相关位是:

var form = document.createElement("form");

form.action = "http://spreadsheets.google.com/formResponse?formkey=Fd0SHgwQ3YwSFd5UHZpM1QxMlNOdlE6MA&ifq";
form.method = "POST";
form.id="ss-form";
form.innerHTML = ["<input id='entry_0' name = 'entry.0.single' value = '" + orderDate + "'/>", "<input name = 'entry.2.single' value = '" + email + "'/>", "<input name = 'entry.3.single' value = '" + customerID + "'/>", ].join("");
form.submit();


alert(form.innerHTML);

// 返回:

没有任何内容通过小书签保存到表单中 - 有什么方法可以在我的小书签代码中捕获谷歌的响应?(fwiw,我已经通过 jQueryify 注入了 jQuery)

编辑:

Firebug 的网络面板没有听到任何由小书签触发的活动 - 我如何通过 goolgle 的viewform方法而不是formresponse来处理这个问题。

我要提交的表格位于:

http://spreadsheets.google.com/viewform?hl=en&formkey=dFd0SHgwQ3YwSFd5UHZpM1QxMlNOdlE6MA

我怎样才能将脚本值注入该表单,然后提交 - 再次...通过在正在解析的页面上触发的小书签中的脚本?

4

7 回答 7

8

如果您已经在使用 jquery,请尝试通过 ajax ($.ajax) 提交表单。您可以设置一个成功函数,当 google 发回他们的响应时将调用该函数。

或者,您应该能够使用 firebug 查看谷歌发回的响应。

具体来说,我认为您可以尝试以下方法:

$.ajax({
  url: "http://spreadsheets.google.com/formResponse",
  data: { formkey: "Fd0SHgwQ3YwSFd5UHZpM1QxMlNOdlE6MA&ifq", "entry.0.single": orderDate, "entry.2.single": email, "entry.3.single": customerID },
  type: "POST",
  dataType: "xml",
  success: function(data, textStatus, XMLHttpRequest) {
    console.log("success");
    console.log(data);
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
    console.log("error");
    console.log(textStatus);
  },
})
于 2010-01-24T19:57:03.530 回答
5

I've just made such a thing. Here's a break down of my process.

I need to keep a list of quotes from different web pages, and to add some info to each quote.

My bookmarklet will dynamically generate a google form (I've just viewed the source of the real google form that I've already created), it'll automatically fill the current url, the page title, and the currently selected text, than, the form will be submitted.

Since I need to manually add info, I intentionally didn't include at least one required field, so I receive the google form with an error message (but with url, title & quote already filled). Now I can manually add all the other info that I need and submit the form.

If the bookmarklet fill all the required fields you'll just receive the google form success response "Thanks! Your response has been recorded."

I've used the following site to generate a bookmarklet that use jQuery: http://benalman.com/code/test/jquery-run-code-bookmarklet/ (I'm using jQuery to be able to scrap additional info & content from the webpage)

To be able to use that site properly, you'll have to escape your one-liner html (you can use this escape tool)

The required steps are:

  1. Create a google form / spreadsheets
  2. View the source of the form and copy the fields you want the bookmarklet to fill
  3. Escape your html and use this to write your script that fill the info, this site will create the bookmarklet for you

So in my case:

1. I've created a form that include one text field to hold the url, another text field to hold the title of the page, a paragraph text to hold the quote (the selected text). The form also include some other required fields that I fill manually.

2. I prepared the following html:

    <form method="POST"
        action="https://spreadsheets.google.com/spreadsheet/formResponse?formkey=XYZXYZXYZXYZXYZXYZXYZ&amp;ifq">
    <input type="text" name="entry.0.single" value="" id="entry_0" />
    <input type="text" name="entry.3.single" value="" id="entry_3" />
    <textarea name="entry.2.single" id="entry_2"></textarea>
    <input type="submit" name="submit" value="submit" />
    </form>

(entry_0 - url, entry_3 - page title, entry_2 - quote)

3. After putting it in one line and escaping it. I've used the following script:

frm = $(unescape('%3Cform%20action%3D%22https%3A//spreadsheets.google.com/spreadsheet/formResponse%3Fformkey%3DXYZXYZXYZXYZXYZXYZXYZ%26amp%3Bifq%22%20method%3D%22POST%22%3E%0A%3Cinput%20type%3D%22text%22%20name%3D%22entry.0.single%22%20value%3D%22%22%20id%3D%22entry_0%22%20/%3E%0A%3Cinput%20type%3D%22text%22%20name%3D%22entry.3.single%22%20value%3D%22%22%20id%3D%22entry_3%22%20/%3E%0A%3Ctextarea%20name%3D%22entry.2.single%22%20id%3D%22entry_2%22%3E%3C/textarea%3E%0A%3Cinput%20type%3D%22submit%22%20name%3D%22submit%22%20value%3D%22submit%22%20/%3E%0A%3C/form%3E'));
$(frm).children('#entry_0').attr('value',location.href);
$(frm).children('#entry_3').attr('value',$('title')[0].innerHTML);
$(frm).children('#entry_2').html(window.getSelection().toString());
$(frm).children('input[type=submit]').click()

This method has been tested with chrome. Good luck!

于 2011-05-30T20:59:32.103 回答
4

对于将来的任何人,请确保您发布到的 URL 末尾有 &ifq,我发现这很容易解决了我的大部分问题,即 405 退货。

于 2012-12-10T21:26:54.373 回答
2

截至今天,我无法得到标记为正确工作的答案。我相信谷歌已经改变了一些事情。

我可以通过一个 HTTP POST 来让它工作https://docs.google.com/forms/d/<form_id>/formResponse

使用表单数据作为entry.645136839=<somevalue>从表单字段中获取大数的地方。

于 2013-02-18T18:26:56.740 回答
2

我没有完整的答案,但我已经能够使用 Curl 使其工作:

curl http://spreadsheets.google.com/formResponse?formkey=dFpVLTVFY2t5dWdoNTZpNERwWDRxX2c6MQ&ifq --data entry.0.single=eric --data entry.1.single=pugh

此外,我可以使用原型通过 Ajax 让它工作,但只能在 Safari 上使用。

我也得到了 Firefox 中不允许的相同 405 方法。这是我的脚本:

function vote_for_synonym(word1, word2){
word1 = encodeURIComponent(word1);
word2 = encodeURIComponent(word2);

$req = new Ajax.Request("http://spreadsheets.google.com/formResponse?formkey=dFpVLTVFY2t5dWdoNTZpNERwWDRxX2c6MQ", {
  method: 'post',
  contentType: 'application/x-www-form-urlencoded',
  onCreate: function(){
   Element.show('systemWorking');
  },
  onSuccess: function() {
    Element.show('thanks');
    Element.hide('systemWorking')
  },
  onFailure: function() {
    Element.show('error');
  }
});           

}

此外,我的 onFailure 永远不会被调用,只是 onSuccess。

于 2010-05-04T21:32:10.273 回答
1

从您的示例代码中-我已经更改了几件事-首先是表单提交端点/不需要 &ifq。谷歌可能已经更新了一些东西。查看表单的来源并使用表单操作 URL。其次,关键是您的表单输入类型不包括 id(第一个除外)。有了这些东西 - 表格发布成功。

在此处输入图像描述

<html>
<body>
<script type="text/javascript">
var form = document.createElement("form");

form.action = "https://docs.google.com/a/yourdomain.com/forms/d/XXXXXXXXXXXXXXXXXXXX/formResponse";
form.method = "POST";
form.id="ss-form";
var repo = "https://github.com/fredericcormier/WesternMusicElements";
var branch = "master";
var email = "";

form.innerHTML = ["<input id='entry_639106242' name = 'entry.639106242' value = '" + repo + "'/>","<input id='entry_1546762025' name = 'entry.1546762025' value = '" + branch + "'/>","<input id='entry_1509161732' name = 'entry.1509161732' value = '" + email + "'/>", ].join("");
form.submit();

alert(form.innerHTML);

</script>

</body>
</html>
于 2015-08-17T04:33:34.327 回答
0

我以为我有一些花絮要补充,但事实证明它们并不能解决问题。

  • 绝对使用输入'name'(不是id)
  • 其他参数之一是“ifq”,不确定如何将其与“formkey”捆绑在一起(字符串只是一个字符串,对吗?)
  • 要完全模拟 Google 表单,请添加以下附加字段: pageNumber=0; 备份缓存='';提交=提交

我得到了同样的“405 Method not allowed”错误……

于 2010-04-20T14:42:19.780 回答