Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
1.4 kB
2
Indexable
Never
var isProductPage = !!document.getElementById("ProductDetailMain");
if (navigator.share && isProductPage) {
  var shareButton = document.createElement("button");
  shareButton.id = "shareButton";
  shareButton.innerHTML = "Share";
  shareButton.style = "background: #000000; border-radius: 25px; padding: 0 28px; color: white; margin-top: 5px; height: 30px; margin-left: 10%; width: 200px;"
//   const parentEl = document.getElementsByClassName("basketBtn")[0]
//   parentEl.appendChild(shareButton);
    document.body.appendChild(shareButton);
    // var basketBtn = document.querySelector('.basketBtn');
    // if (basketBtn) {
    //     basketBtn.parentNode.insertBefore(shareButton, basketBtn.nextSibling); // Insert share button after basketBtn

    //     // Add some spacing between the basketBtn and shareButton
    //     shareButton.style.marginTop = "10px";  // Adjust as needed
    // }


  document.getElementById("shareButton").addEventListener("click", function() {
      if (navigator.share) {
          navigator.share({
              title: document.title,
              url: window.location.href
          }).then(() => {
              console.log('Thanks for sharing!');
          }).catch(err => {
              console.log('Error sharing:', err);
          });
      } else {
          console.log("Your browser doesn't support the Web Share API. Try copying the URL directly.");
      }
  });
}