从页面上的元素中获取URL,以缩短Bitly URL

时间:2018-06-22 18:23:35

标签: javascript shopify bit.ly

我在shopify工作-尝试在客户端进行

我有一个正在生成的URL(基于当前购物车中的商品),该URL根据商品的ID#将商品添加到购物车中。

我正在为我们的销售团队打造这件小东西,以便他们可以为客户下订单并通过URL将安排发送给某人-现在在shopify中,如果您按照他们的方式去做,将会带给客户结帐窗口,他们无法编辑该订单-这样,我们只是在购物车中发送了一个安排,他们可以在实际结账之前进行调整。

现在,根据购物车中的商品数量,该网址会变得非常长,我想使用bit.ly根据生成的网址创建一个简短的网址-我现在有了它,以便它可以对URL进行编码,因此其中不会包含任何奇怪的字符-但是,在查看有关api的文档时,大多数示例似乎都是通用的,而堆栈溢出的其他情况似乎是特定于其问题的-

也许无法完成?感谢您抽出宝贵的时间阅读本文,如果有人有任何建议-或者您认为我只是错过了很多明显的内容,请随时告诉我。如果可以轻松了解我要执行的操作,那么我可以提供到目前为止的代码!

该页面的屏幕截图

screenshot

----在下面添加代码----

// get the cart
if (typeof Shopify === 'undefined') var Shopify = {};
Shopify.cart = {{ cart | json }};
Shopify.idsInCart = [];
Shopify.quanInCart = [];


//where we gonna put the url
var cartURL = document.getElementById('cart_url');

// for every item in Shopify Cart - push to idsInCart and print the IDs to the cart url
for (var i=0; i<Shopify.cart.items.length; i++) {
  Shopify.idsInCart.push(Shopify.cart.items[i].id);
  cartURL.innerHTML += 'id[]=' + Shopify.idsInCart[i] + '&';  
}
// get the div with cartURLform as an id
var longUrlNode = document.getElementById('cartURLform'),
    // grab the .textContent from that div
    textContent = longUrlNode.textContent;

// 
var uri = longUrlNode.textContent;
var res = encodeURI(uri);



// Copy to clipboard example
document.querySelector("#qlink").onclick = function() {
  // Select the content
  document.querySelector("#qlink").select();
  // Copy to the clipboard
  document.execCommand('copy');
};



(function(long_url,callback){
  bi = new URL("https://api-ssl.bitly.com/v3/shorten?");
  var params = [
    "login=__obviously__",
    "domain=bit.ly",
    "apiKey=__obviously__",
    "longUrl="+ encodeURIComponent(long_url)
  ]
  bi.search = "?"+params.join('&')
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange=function () {
    if (xhr.readyState === 4) {
      if (xhr.status === 200) {

        var res = JSON.parse(xhr.responseText);
        callback(res["data"]["url"]);

        //           document.getElementById("qlink").value = JSON.parse(xhr.responseText);


      } else {
        alert('There was a problem with the request.');
      }
    }
  }
  xhr.open("GET",bi.toString());
  xhr.send(null)

})(res,function(a){

  //       prompt("hello", a);
  document.getElementById("qlink").value = a;

});

---编辑以添加代码

0 个答案:

没有答案