Bookmarklet在Google电子表格中保存网址 [英] Bookmarklet to save URL in Google Spreadsheet

查看:110
本文介绍了Bookmarklet在Google电子表格中保存网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的小书签,它抓取当前网页location.ref的URL并将其保存在Google Spreadsheet中。保存后,我想保留在当前网页上。

I want to create a simple bookmarklet, that grabs the URL of the current webpage "location.ref" and saves it in a Google Spreadsheet. After it saves it, I want to stay on the current webpage.

我知道写入Google Spreadsheet的唯一方法是使用Google App Script。所以我写了一个简单的脚本来做到这一点:

The only way I know of writing to Google Spreadsheet is using Google App Script. So I wrote a simple script that does just that:

function doGet(request) {
  var ss = SpreadsheetApp.openByUrl( "https://docs.google.com/spreadsheet/ccc?key=<MY-SPREADSHEET-ID>");

 var sheet = ss.getSheets()[0];
 var headers = ["Timestamp", "url"];

 var nextRow = sheet.getLastRow(); 
 var cell = sheet.getRange('a1');
 var col = 0;
 for (i in headers){
    if (headers[i] == "Timestamp"){
       val = new Date();
    } else {
       val = request.parameter[headers[i]]; 
    }
    cell.offset(nextRow, col).setValue(val);
    col++;
 }



   return ContentService.createTextOutput(request.parameter.url)
   .setMimeType(ContentService.MimeType.TEXT);
}

我将其作为web应用发布。我写了书签:

I published this as a webapp. I wrote the bookmarklet:

<a href="javascript:(

   function(){ 
     alert(window.open('https://script.google.com/macros/s/<MYWEBAPP>/exec?url='+encodeURIComponent(location.href), '_self')); 

   }

  )();"> 

BOOKMARK

</a>

到目前为止这么好。实际上,当我点击书签时,它确实能够抓取当前网页的网址并将其保存在我的电子表格中。但是,然后,webapp返回一个文本响应,书签显示文本,导致我离开我的当前网站。

So far so good. It actually works when I click on the bookmarklet, it does grab the URL of the current webpage and save it in my spreadsheet. But then, the webapp returns a text response and the bookmarklet displays the text causing me to move away from my current website.

有没有办法忽略响应? GAS webapp脚本要求我使用doGet(),它必须返回一些东西。有没有办法从GAS脚本中返回任何东西?另外,有没有办法我可以使用其他调用来替换window.open来调用web应用程序,它允许我将响应存储在变量中并忽略它?

Is there a way to ignore the response? GAS webapp script requires me to use doGet() that has to return something. Is there a way to not return anything from GAS script? Alternatively, is there a way i could use some other call to replace window.open to invoke the webapp that would allow me to store the response in a variable and ignore it?

推荐答案

_self 更改为其他内容,例如书签,它会在新窗口或标签中打开。如果你在很多页面上使用它们,如果它们保持相同的名称,它们将全部重复使用相同的标签。

Change _self to something else, e.g. bookmarker and it will open in a new window or tab. If you use it on many pages, they will all reuse the same tab if it keeps the same name.

这篇关于Bookmarklet在Google电子表格中保存网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆