Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="center-center">
  <button>Share<i class="fas fa-share-alt"></i></button>
  <p class="result"></p>
</div>
<input type="hidden" id="content-to-copy" />

              
            
!

CSS

              
                // The style of button is modified from https://codepen.io/ayoisaiah/pen/YbNazJ

html, body {
  background-color: #dedede;
  height: 100%;
}

.center-center {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  flex-direction: column;
}
button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: auto;
  padding: 8px 30px;
  color: #777;
  text-align: center;
  font-size: 14px;
  font-weight: 500;
  line-height: 1.1;
  letter-spacing: 2px;
  text-transform: capitalize;
  text-decoration: none;
  white-space: nowrap;
  border-radius: 4px;
  border: 1px solid #ddd;
  cursor: pointer;
  background-color: white;
}

button:hover {
  border-color: #cdd;
}

.fa-share-alt {
  padding-left: 5px;
}



              
            
!

JS

              
                // 選擇和 DOM 有關的元素
const btn = document.querySelector("button");
const result = document.querySelector(".result");

// 當使用者點擊分享時要帶入的資訊
const shareData = {
  url: "https://pjchender.blogspot.com", // 要分享的 URL
  title: "PJCHENder 那些沒告訴你的小細節", // 要分享的標題
  text: "好多眉眉角角啊" // 要分享的文字內容
};

// 當使用者點擊按鈕時
btn.addEventListener("click", () => {
  if (navigator.share) {
    handleNavigatorShare();
  } else {
    handleNotSupportNavigatorShare();
  }
});

async function handleNavigatorShare() {
  try {
    // 使用 Web Share API
    await navigator.share(shareData);
    result.textContent = "感謝你的的分享";
  } catch (err) {
    // 使用者拒絕分享或發生錯誤
    const { name, message } = err;
    if (name === "AbortError") {
      result.textContent = "您已取消分享此訊息";
    } else {
      result.textContent = err;
      console.log("發生錯誤", err);
    }
  }
}

function handleNotSupportNavigatorShare() {
  let contentToCopy = document.querySelector("#content-to-copy");
  contentToCopy.value = shareData.url;
  contentToCopy.setAttribute("type", "text"); // 不是 hidden 才能複製
  contentToCopy.select();

  try {
    var successful = document.execCommand("copy");
    var msg = successful ? "成功" : "失敗";
    alert(`${shareData.url} - 複製${msg}`);
  } catch (err) {
    alert("Oops, unable to copy");
  }

  /* unselect the range */
  contentToCopy.setAttribute("type", "hidden");
  window.getSelection().removeAllRanges();
}

              
            
!
999px

Console