📜 [專欄新文章] [ZKP 讀書會] Trust Token Browser API
✍️ Yuren Ju
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium
Trust Token API 是一個正在標準化的瀏覽器 API,主要的目的是在保護隱私的前提下提供跨站授權 (Cross-domain authorization) 的功能,以前如果需要跨站追蹤或授權通常都使用有隱私疑慮的 Cookies 機制,而 Trust Token 則是希望在保護隱私的前提下完成相同的功能。
會在 ZKP (Zero-knowledge proof) 讀書會研究 Trust Token 主要是這個 API 採用了零知識證明來保護隱私,這也是這次讀書會中少見跟區塊鏈無關的零知識證明應用。
問題
大家應該都有點了一個產品的網頁後,很快的就在 Facebook 或是 Google 上面看到相關的廣告。但是產品網頁並不是在 Facebook 上面,他怎麼會知道我看了這個產品的頁面?
通常這都是透過 Cookie 來做跨網站追蹤來記錄你在網路上的瀏覽行為。以 Facebook 為例。
當使用者登入 Facebook 之後,Facebook 會透過 Cookie 放一段識別碼在瀏覽器裡面,當使用者造訪了有安裝 Facebook SDK 來提供「讚」功能的網頁時,瀏覽器在載入 SDK 時會再度夾帶這個識別碼,此時 Facebook 就會知道你造訪了特定的網頁並且記錄下來了。如此一來再搭配其他不同管道的追蹤方式,Facebook 就可以建構出特定使用者在網路上瀏覽的軌跡,從你的瀏覽紀錄推敲喜好,餵給你 Facebook 最想給你看的廣告了。
不過跨站追蹤也不是只能用在廣告這樣的應用上,像是 CDN (Content Delivery Network) 也是一個應用場景。CDN 服務 Cloudflare 提供服務的同時會利用 Captcha 先來確定進入網站的是不是真人或是機器人。而他希望使用者如果是真人時下次造訪同時也是採用 Cloudflare 服務的網站不要再跳出 Captcha 驗證訊息。
雖然 Cloudflare 也需要跨站驗證的功能來完成他們的服務,但是相較於 Google 或 Facebook 來說他們是比較沒那麼想知道使用者的隱私。有沒有什麼辦法可以保護使用者隱私的狀況下還能完成跨站驗證呢?
這就是今天要講的新 API: Trust Token。
Trust Token API - The Chromium Projects
Trust Token / Privacy Pass 簡介
Trust Token 其實是由 Privacy Pass 延伸而來。Privacy Pass 就是由 Cloudflare 所開發的實驗性瀏覽器延伸套件實作一個驗證機制,可以在不透漏過多使用者隱私的前提下實作跨站驗證。而 Trust Token 則是標準化的 Privacy Pass,所以兩個運作機制類似,但是實作方式稍有不同。
先看一下 Privacy Pass 是如何使用。因為這是實驗性的瀏覽器延伸套件所以看起來有點陽春,不過大致上還是可以了解整個概念。
以 hCaptcha 跟 Cloudflare 的應用為例,使用者第一次進到由 Cloudflare 提供服務的網站時,網站會跳出一些人類才可以解答的問題比如說「挑出以下是汽車的圖片」。
當使用者答對問題後,Cloudflare 會回傳若干組 blind token,這些 blind token 還會需要經過 unblind 後才會變成真正可以使用的 token,這個過程為 issue token。如上圖所示假設使用者這次驗證拿到了 30 個 token,在每次造訪由 Cloudflare 服務的網站時就會用掉一個 token,這個步驟稱為 redeem token。
但這個機制最重要的地方在於 Cloudflare 並無法把 issue token 跟 redeem token 這兩個階段的使用者連結在一起,也就是說如果 Alice, Bob 跟 Chris 都曾經通過 Captcha 測試並且獲得了 Token,但是在後續瀏覽不同網站時把 token 兌換掉時,Clouldflare 並無法區分哪個 token 是來自 Bob,哪個 token 是來自 Alice,但是只要持有這種 token 就代表持有者已經通過了 Captcha 的挑戰證明為真人。
但這樣的機制要怎麼完成呢?以下我們會透過多個步驟的例子來解釋如何達成這個目的。不過在那之前我們要先講一下 Privacy Pass 所用到的零知識證明。
零知識證明 (Zero-knowledge proof)
零知識證明是一種方法在不揭露某個祕密的狀態下,證明他自己知道那個秘密。
Rahil Arora 在 stackexchange 上寫的比喻我覺得是相對好理解的,下面簡單的翻譯一下:
假設 Alice 有超能力可以幾秒內算出樹木上面有幾片樹葉,如何在不告訴 Bob 超能力是怎麼運作並且也不告訴 Bob 有多少片葉子的狀況下證明 Alice 有超能力?我們可以設計一個流程來證明這件事情。
Alice 先把眼睛閉起來,請 Bob 選擇拿掉樹上的一片葉子或不拿掉。當 Alice 睜開眼睛的時候,告訴 Bob 他有沒有拿掉葉子。如果一次正確的話確實有可能是 Alice 幸運猜到,但是如果這個過程連續很多次時 Alice 真的擁有數葉子的超能力的機率就愈來愈高。
而零知識證明的原理大致上就是這樣,你可以用一個流程來證明你知道某個秘密,即使你不真的揭露這個秘密到底是什麼,以上面的例子來說,這個秘密就是超能力運作的方式。
以上就是零知識證明的概念,不過要完成零知識證明有很多各式各樣的方式,今天我們要介紹的是 Trust Token 所使用的零知識證明:DLEQ。
DLEQ (Discrete Logarithm Equivalence Proof)
說明一下以下如果小寫的變數如 c, s 都是純量 (Scalar),如果是大寫如 G, H則是橢圓曲線上面的點 (Point),如果是 vG 則一樣是點,計算方式則是 G 連續相加 v 次,這跟一般的乘法不同,有興趣可以程式前沿的《橢圓曲線加密演算法》一文解釋得比較詳細。
DLEQ 有一個前提,在系統中的所有人都知道公開的 G 跟 H 兩個點,此時以下等式會成立:
假設 Peggy 擁有一個秘密 s 要向 Victor 證明他知道 s 為何,並且在這個過程中不揭露 s 真正的數值,此時 Victor 可以產生一個隨機數 c 傳送給 Peggy,而 Peggy 則會再產生一個隨機數 v 並且產生 r,並且附上 vG, vH, sG, sH:
r = v - cs
所以 Victor 會得到 r, sG, sH, vG, vH 再加上他已經知道的 G, H。這個時候如果 Victor 計算出以下兩個等式就代表 Peggy 知道 s 的真正數值:
vG = rG + c(sG)vH = rH + c(sH)
我們舉第二個等式作為例子化簡:
vH = rH + c(sH) // 把 r 展開成 v - csvH = (v - cs)H + c(sH) // (v - cs)H 展開成 vH - csHvH = vH - c(sH) + c(sH) // 正負 c(sH) 消掉vH = vH
這樣只有 Peggy 知道 s 的狀況下才能給出 r,所以這樣就可以證明 Peggy 確實知道 s。
從簡易到實際的情境
Privacy Pass 網站上透過了循序漸進的七種情境從最簡單的假設到最後面實際使用的情境來講解整個機制是怎麼運作的。本文也用相同的方式來解釋各種情境,不過前面的例子就會相對比較天真一點,就請大家一步步的往下看。
基本上整個過程是透過一種叫做 Blind Signature 的方式搭配上零知識證明完成的,以下參與的角色分為 Client 與 Server,並且都會有兩個階段 issue 與 redeem token。
Scenario 1
如果我們要設計一個這樣可以兌換 token 來確認身分的系統,其中有一個方法是透過橢圓曲線 (elliptic curve) 完成。Client 挑選一個在橢圓曲線上的點 T 並且傳送給 Server,Server 收到後透過一個只有 Server 知道的純量 (scalar) s 對 T 運算後得到 sT 並且回傳給 Client,這個產生 sT 的過程稱為 Sign Point,不過實際上運作的原理就是橢圓曲線上的連續加法運算。
SignPoint(T, s) => sT
等到 Client 需要兌換時只要把 T 跟 sT 給 Server,Server 可以收到 T 的時候再 Sign Point 一次看看是不是 sT 就知道是否曾經 issue 過這個 token。
Issue
以下的範例,左邊都是 Client, 右邊都是 Server。 -> 代表 Client 發送給 Server,反之亦然。
// Client 發送 T 給 Server, 然後得到 sT
T -> <- sT
Redeem
// Client 要 redeem token 時,傳出 T 與 sT
T, sT ->
問題:Linkability
因為 Server 在 issue 的時候已經知道了 T,所以基本上 Server 可以透過這項資訊可以把 issue 階段跟 redeem 階段的人連結起來進而知道 Client 的行為。
Scenario 2
要解決上面的問題,其中一個方法是透過 Blind Signature 達成。Client 不送出 T,而是先透過 BlindPoint 的方式產生 bT 跟 b,接下來再送給 Server bT。Server 收到 bT 之後,同樣的透過 Sign Point 的方式產生結果,不一樣的地方是情境 1 是用 T,而這邊則用 bT 來作 Sign Point,所以得出來的結果是 s(bT)。
Client:BlindPoint(T) => (bT, b)
Server:SignPoint(bT, s) => sbT
而 Blind Signature 跟 Sign Point 具備了交換律的特性,所以得到 s(bT) 後可以透過原本 Client 已知的 b 進行 Unblind:
UnblindPoint(sbT, b) => sT
這樣一來在 Redeem 的時候就可以送出 T, sT 給 Server 了,而且透過 SignPoint(T, s) 得出結果 sT’ 如果符合 Client 傳來的 sT 就代表確實 Server 曾經簽過這個被 blind 的點,同時因為 T 從來都沒有送到 Server 過,所以 Server 也無法將 issue 與 redeem 階段的 Client 連結在一起。
Issue
bT -> <- s(bT)
Redeem
T, sT ->
問題:Malleability
以上的流程其實也有另外一個大問題,因為有交換律的關係,當 Client 透過一個任意值 a 放入 BlindPoint 時產生的 a(sT) 就會等於 s(aT):
BlindPoint(sT) => a(sT), a// a(sT) === s(aT)
此時如果將 aT 跟 s(aT) 送給 Server Redeem,此時因為
SignPoint(aT, s) => s(aT)
所以就可以兌換了,這樣造成 Client 可以無限地用任意數值兌換 token。
Scenario 3
這次我們讓 Client 先選擇一個純數 t,並且透過一種單向的 hash 方式來產生一個在橢圓曲線上的點 T,並且在 redeem 階段時原本是送出 T, sT 改成送出 t, sT。
因為 redeem 要送出的是 t,上個情境時透過任意數 a 來產生 s(aT) 的方法就沒辦法用了,因為 t 跟 sT 兩個參數之間並不是單純的再透過一次 BlindPoint() 就可以得到,所以就沒辦法無限兌換了。
Issue
T = Hash(t) bT -> <- sbT
Redeem
t, sT ->
問題:Redemption hijacking
在這個例子裏面,Client 其實是沒有必要傳送 sT 的,因為 Server 僅需要 t 就可以計算出 sT,額外傳送 sT 可能會導致潛在的 Redemption hijacking 問題,如果在不安全的通道上傳輸 t, sT 就有可能這個 redemption 被劫持作為其他的用途。
不過在網站上沒講出實際上要怎麼利用這個問題,但是少傳一個可以計算出來的資料總是好的。Client 只要證明他知道 sT 就好,而這可以透過 HMAC (Hash-based Message Authentication Code) 達成。
Scenario 4
步驟跟前面都一樣,唯一不一樣的地方是 redeem 的時候原本是傳 t, sT,現在則改傳 t, M, HMAC(sT, M),如果再介紹 HMAC 篇幅會太大,這邊就不解釋了,但可以是作是一個標準的 salt 方式讓 Hash 出來的結果不容易受到暴力破解。
這樣的特性在這個情境用很適合,因為 Server 透過 t 就可以計算出 sT,透過公開傳遞的 M 可以輕易地驗證 client 端是否持有 sT。
Issue
T = Hash(t) bT -> <- sbT
Redeem
t, M, HMAC(sT, M) ->
問題:Tagging
這邊的問題在於 Server 可以在 issue 階段的時候用不一樣的 s1, s2, s3 等來發出不一樣的 sT’,這樣 Server 在 Redeem 階段就可以得知 client 是哪一個 s。所以 Server 需要證明自己每次都用同樣的 s 同時又不透漏 s 這個純亮。
要解決這個問題就需要用到前面我們講解的零知識證明 DLEQ 了。
Scenario 5
前面的 DLEQ 講解有提到,如果有 Peggy 有一個 s 秘密純量,我們可以透過 DLEQ 來證明 Peggy 知道 s,但是又不透漏 s 真正的數值,而在 Privacy Pass 的機制裡面,Server 需要證明自己每次都用 s,但是卻又不用揭露真正的數值。
在 Issue 階段 Client 做的事情還是一樣傳 bT 給 Server 端,但 Server 端的回應就不一樣了,這次 Server 會回傳 sbT 與一個 DLEQ 證明,證明自己正在用同一個 s。
首先根據 DLEQ 的假設,Server 會需要先公開一組 G, H 給所有的 Client。而在 Privacy Pass 的實作中則是公開了 G 給所有 Client,而 H 則改用 bT 代替。
回傳的時候 Server 要證明自己仍然使用同一個 s 發出 token,所以附上了一個 DLEQ 的證明 r = v - cs,Client 只要算出以下算式相等就可證明 Server 仍然用同一個 s (記住了 H 已經改用 bT 代替,此時 client 也有 sbT 也就是 sH):
vH = rH + c(sH) // H 換成 bTvbT = rbT + c(sbT) // 把 r 展開成 v - csvbT = (v - cs)bT + c(sbT) // (v - cs)bT 展開成 vbT - csbTvbT = vbT - c(sbT) + c(sbT) // 正負 c(sbT) 消掉vbT = vbT
這樣就可以證明 Server 依然用同一個 s。
Issue
T = Hash(t) bT -> <- sbT, DLEQ(bT:sbT == G:sG)
Redeem
t, M, HMAC(sT, M) ->
問題:only one redemption per issuance
到這邊基本上 Privacy Pass 的原理已經解釋得差不多了,不過這邊有個問題是一次只發一個 token 太少,應該要一次可以發多個 token。這邊我要跳過源文中提到的 Scenario 6 解釋最後的結果。
Scenario 7
由於一次僅產生一個 redeem token 太沒效率了,如果同時發很多次,每次都產生一個 proof 也不是非常有效率,而 DLEQ 有一個延伸的用法 “batch” 可以一次產生多個 token, 並且只有使用一個 Proof 就可以驗證所有 token 是否合法,這樣就可以大大的降低頻寬需求。
不過這邊我們就不贅述 Batch DLEQ 的原理了,文末我會提及一些比較有用的連結跟確切的源碼片段讓有興趣的人可以更快速的追蹤到源碼片段。
Issue
T1 = Hash(t1) T2 = Hash(t2)T3 = Hash(t3)b1T1 ->b2T2 ->b3T3 -> c1,c2,c3 = H(G,sG,b1T1,b2T2,b3T3,s(b1T1),s(b2T2),s(b3T3)) <- sb1T1 <- sb2T2 <- sb3T3 <- DLEQ(c1b1T1+c2b2T2+c3b3T3:s(c1b1T1+c2b2T2+c3b3T3) == G: sG)
Redeem
t1, M, HMAC(sT1, M) ->
結論
Privacy Token / Trust Token API 透過零知識證明的方式來建立了一個不需要透漏太多隱私也可以達成跟 cookie 相同效果的驗證方式,期待可以改變目前許多廣告巨頭透過 cookie 過分的追蹤使用者隱私的作法。
不過我在 Trust Token API Explainer 裡面看到這個協議裡面的延伸作法還可以夾帶 Metadata 進去,而協議制定的過程中其實廣告龍頭 Google 也參與其中,希望這份協議還是可以保持中立,盡可能地讓最後版本可以有效的在保護隱私的情況下完成 Cross-domain authorization 的功能。
參考資料
IETF Privacy Pass docs
Privacy Pass: The Protocol
Privacy Pass: Architectural Framework
Privacy Pass: HTTP API
Cloudflare
Supporting the latest version of the Privacy Pass Protocol (cloudflare.com)
Chinese: Cloudflare支持最新的Privacy Pass扩展_推动协议标准化
Other
Privacy Pass official website
Getting started with Trust Tokens (web.dev)
WICG Trust Token API Explainer
Non-interactive zero-knowledge (NIZK) proofs for the equality (EQ) of discrete logarithms (DL) (asecuritysite.com) 這個網站非常實用,列了很多零知識證明的源碼參考,但可惜的是 DLEQ 這個演算法講解有錯,讓我在理解演算法的時候撞牆很久。所以使用的時候請多加小心,源碼應該是可以參考的,解釋的話需要斟酌一下。
關鍵源碼
這邊我貼幾段覺得很有用的源碼。
privacy pass 提供的伺服器端產生 Proof 的源碼
privacy pass 提供的瀏覽器端產生 BlindPoint 的源碼
github dedis/kyber 產生 Proof 的源碼
[ZKP 讀書會] Trust Token Browser API was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.
👏 歡迎轉載分享鼓掌
同時也有10000部Youtube影片,追蹤數超過2,910的網紅コバにゃんチャンネル,也在其Youtube影片中提到,...
「originally用法」的推薦目錄:
- 關於originally用法 在 Taipei Ethereum Meetup Facebook 的最佳貼文
- 關於originally用法 在 L'Arc-en-Ciel Facebook 的精選貼文
- 關於originally用法 在 Taipei Ethereum Meetup Facebook 的精選貼文
- 關於originally用法 在 コバにゃんチャンネル Youtube 的最讚貼文
- 關於originally用法 在 大象中醫 Youtube 的精選貼文
- 關於originally用法 在 大象中醫 Youtube 的最佳貼文
- 關於originally用法 在 [請益] 原本打算做,但後來沒有的用法(want) - 看板Eng-Class 的評價
- 關於originally用法 在 A2 GMAT | 想確認一下句尾with獨立結構的用法~是否在with ... 的評價
- 關於originally用法 在 YouTube 嵌入式播放器及播放器参数 的評價
- 關於originally用法 在 解析德文irgend 意思和用法 - YouTube 的評價
originally用法 在 L'Arc-en-Ciel Facebook 的精選貼文
[A Moment in L'Arc-en-Ciel History] If you're a French fan of L'Arc-en-Ciel, you've probably seen "LIVE IN PARIS," a live concert video originally released in May 2009. The video is a full recording of the band's historic show at Le Zénith in Paris on May 9th, 2008, the first Paris solo performance by any Japanese artist. The members were thrilled to be performing there, which can be seen between songs when they speak to the audience in French!
[A Moment in L'Arc-en-Ciel History] 在法國的L'Arc-en-Ciel歌迷有可能知道『LIVE IN PARIS』這張於2009年5月發行的演唱會DVD。該作品完整收錄了樂團2008年5月9日在巴黎的天頂劇院所進行的歷史性演出,而他們也是成為了史上第一個在法國進行個人演唱會的日本藝人。成員們對於能在這樣的場地演出表現得十分興奮,從他們在演出間隙和觀眾們積極用法語互動就可以看出來這一點了!
originally用法 在 Taipei Ethereum Meetup Facebook 的精選貼文
📜 [專欄新文章] Solidity Weekly #8
✍️ mingderwang
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium
Events 和 Logs 的用法
從 #8 開始, 我想增加短連結方式(links 分享), 介紹大家更多有關 Solidity 的好文章. 若有想共筆的人, 也可以將連結與短文介紹, 寄到 twitter @mingderwang 給我, 我會放在下一期的 Solidity Weekly 刊登.
TL;DR
事實上 solidity 本身只有 event 指令, 沒有 log 指令(註: log0, log1 …它們屬於 assembly 用的 low-level function). Events 是用來實現 logs 的方法, 把它想成是比記載在 storage 裡更便宜的資料儲存方法. 但 logs 只能從區塊鏈的外部, 比如說用 web3.js 來讀取, 正確的說法應該是 trigger 的方式, 透由 web3.js 的 watch 指令來看到 ”事件” 的發生, 所以也可以用來更新 Dapp 的畫面.
這裡提到比記載在 storage 裡更便宜, 指的是相同的資料量, 放在記憶體裡當然貴了. 在區塊鏈的世界裡, 用到 miner 電腦的資源越多, 所要花費的 gas 就越高. LOG 運算碼(opcode)本身需花 375 gas, 再加上每個 byte 只要花 8 gas, 相較於 contract storage 每 32 bytes 就要 20,000 gas 實在差很大.
最後, 要提到的是 event 定義裡的參數(如下範例的 _market 和 _sender), 可以加 indexed 保留字讓該參數可以當參數被查詢, 又稱為 topics. 但最多只能用 3 個 (其實原本有四個 topics, 但第一個已經被 event 的識別值給用掉了). 如此一來 web3.js 就可以針對某個 _market 或 _sender 的值來查詢. 沒有用 indexed 的只能當 data 讀取. (若要 indexed 更詳細用法, 可參考 這裡)
contract CryptoExchange {
event Deposit(uint256 indexed _market, address indexed _sender, uint256 _amount, uint256 _time);
function deposit(uint256 _amount, uint256 _market) returns (int256) { // perform deposit, update user’s balance, etc Deposit(_market, msg.sender, _amount, now);}
links 分享;
Solidity撰寫智能合約與注意事項(二) — (NIC Lin)
Technical Introduction to Events and Logs in Ethereum — (ConsenSys)
[無用] 如何在 token 發行量裡埋彩蛋? — (Roger Wu)
後記;
Events 在文法上, 要在 event 的呼叫前面增加 emit 字眼, 否則 solidity compiler 會有 “Invoking events without “emit” prefix is deprecated.” 的警告出現.
*emit 適用於 solidity ^0.4.21;
Solidity Weekly #8 was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.
👏 歡迎轉載分享鼓掌
originally用法 在 A2 GMAT | 想確認一下句尾with獨立結構的用法~是否在with ... 的美食出口停車場
想確認一下句尾with獨立結構的用法~是否在with前方一定要打上逗點,沒有打上逗點的with一律視為一般介系詞,不用想到獨立結構呢? 會有這個問題主要是因為看到OG其中 ... ... <看更多>
originally用法 在 YouTube 嵌入式播放器及播放器参数 的美食出口停車場
Overview
This document explains how to embed a YouTube player in your application and also defines the parameters that are available in the YouTube embedded player.
By appending parameters to the IFrame URL, you can customize the playback experience in your application. For example, you can automatically play videos using the autoplay
parameter or cause a video to play repeatedly using the loop
parameter. You can also use the enablejsapi
parameter to enable the player to be controlled via the IFrame Player API.
This page currently defines all parameters supported in any YouTube embedded player. Each parameter definition identifies the players that support the corresponding parameter.
Note: Embedded players must have a viewport that is at least 200px by 200px. If the player displays controls, it must be large enough to fully display the controls without shrinking the viewport below the minimum size. We recommend 16:9 players be at least 480 pixels wide and 270 pixels tall.
Embed a YouTube playerYou can use any of the following methods to embed a YouTube player in your application and specify player parameters. Note that the instructions below demonstrate how to embed a player that loads a single video. The following section explains how to configure your player to load a playlist or a user's uploaded videos.
Embed a player using an<iframe>
tagDefine an <iframe>
tag in your application in which the src
URL specifies the content that the player will load as well as any other player parameters you want to set. The <iframe>
tag's height
and width
parameters specify the dimensions of the player.
If you create the <iframe>
element yourself (rather than using the IFrame Player API to create it), you can append player parameters directly to the end of the URL. The URL has the following format:
https://www.youtube.com/embed/VIDEO_ID
The <iframe>
tag below would load a 640x360px player that would play the YouTube video M7lc1UVf-VE
. Since the URL sets the autoplay
parameter to 1
, the video would play automatically once the player has loaded.
Embed a player using the IFrame Player API
<iframe id="ytplayer" type="text/html" width="640" height="360"
src="https://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&origin=http://example.com"
frameborder="0"></iframe>
Follow the IFrame Player API instructions to insert a video player in your web page or application after the Player API's JavaScript code has loaded. The second parameter in the constructor for the video player is an object that specifies player options. Within that object, the playerVars
property identifies player parameters.
The HTML and JavaScript code below shows a simple example that inserts a YouTube player into the page element that has an id
value of ytplayer
. The onYouTubePlayerAPIReady()
function specified here is called automatically when the IFrame Player API code has loaded. This code does not define any player parameters and also does not define other event handlers.
Select content to play
<div id="ytplayer"></div><script>
// Load the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); // Replace the 'ytplayer' element with an <iframe> and
// YouTube player after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('ytplayer', {
height: '360',
width: '640',
videoId: 'M7lc1UVf-VE'
});
}
</script>
You can configure your embedded player to load a video, a playlist, or a user's uploaded videos.
The following list explains these options:
Loading a video
For an IFrame embed, the YouTube video ID for the video that you want to load is specified in the IFrame's src
URL.
https://www.youtube.com/embed/VIDEO_ID
If you are using the YouTube Data API (v3), you can programmatically construct these URLs by retrieving video IDs from search results, playlist item resources, video resources, or other resources. After obtaining a video ID, replace the VIDEO_ID
text in the URLs above with that value to create the player URL.
Loading a playlist
Set the listType
player parameter to playlist
. In addition, set the list
player parameter to the YouTube playlist ID that you want to load.
https://www.youtube.com/embed?listType=playlist&list=PLAYLIST_ID
Note that you need to prepend the playlist ID with the letters PL
as shown in the following example:
https://www.youtube.com/embed?listType=playlist&list=PLC77007E23FF423C6
If you are using the YouTube Data API (v3), you can programmatically construct these URLs by retrieving playlist IDs from search results, channel resources, or activity resources. After obtaining a playlist ID, replace the PLAYLIST_ID
text in the URL above with that value.
Loading a user's uploaded videos
Set the listType
player parameter to user_uploads
. In addition, set the list
player parameter to the YouTube username whose uploaded videos you want to load.
https://www.youtube.com/embed?listType=user_uploads&list=USERNAME
Supported parameters
All of the following parameters are optional.
Parameters
autoplay
This parameter specifies whether the initial video will automatically start to play when the player loads. Supported values are
0
or 1
. The default value is 0
.If you enable Autoplay, playback will occur without any user interaction with the player;
playback data collection and sharing will therefore occur upon page load.
cc_lang_pref
This parameter specifies the default language that the player will use to display captions. Set the parameter's value to an ISO 639-1 two-letter language code.
If you use this parameter and also set the
cc_load_policy
parameter to 1
, then the player will show captions in the specified language when the player loads. If you do not also set the cc_load_policy
parameter, then captions will not display by default, but will display in the specified language if the user opts to turn captions on.cc_load_policy
Setting the parameter's value to
1
causes closed captions to be shown by default, even if the user has turned captions off. The default behavior is based on user preference.color
This parameter specifies the color that will be used in the player's video progress bar to highlight the amount of the video that the viewer has already seen. Valid parameter values are
red
and white
, and, by default, the player uses the color red in the video progress bar. See the YouTube API blog for more information about color options.controls
This parameter indicates whether the video player controls are displayed:
controls=0
– Player controls do not display in the player.
controls=1
(default) – Player controls display in the player.
disablekb
Setting the parameter's value to
1
causes the player to not respond to keyboard controls. The default value is 0
, which means that keyboard controls are enabled. Currently supported keyboard controls are:
Spacebar or [k]: Play / Pause
Arrow Left: Jump back 5 seconds in the current video
Arrow Right: Jump ahead 5 seconds in the current video
Arrow Up: Volume up
Arrow Down: Volume Down
[f]: Toggle full-screen display
[j]: Jump back 10 seconds in the current video
[l]: Jump ahead 10 seconds in the current video
[m]: Mute or unmute the video
[0-9]: Jump to a point in the video. 0
jumps to the beginning of the video, 1
jumps to the point 10% into the video, 2
jumps to the point 20% into the video, and so forth.
enablejsapi
Setting the parameter's value to
1
enables the player to be controlled via IFrame Player API calls. The default value is 0
, which means that the player cannot be controlled using that API.For more information on the IFrame API and how to use it, see the IFrame API documentation.
end
This parameter specifies the time, measured in seconds from the start of the video, when the player should stop playing the video. The parameter value is a positive integer.
Note that the time is measured from the beginning of the video and not from either the value of the
start
player parameter or the startSeconds
parameter, which is used in YouTube Player API functions for loading or queueing a video.fs
Setting this parameter to
0
prevents the fullscreen button from displaying in the player. The default value is 1
, which causes the fullscreen button to display.hl
Sets the player's interface language. The parameter value is an ISO 639-1 two-letter language code or a fully specified locale. For example,
fr
and fr-ca
are both valid values. Other language input codes, such as IETF language tags (BCP 47) might also be handled properly.The interface language is used for tooltips in the player and also affects the default caption track. Note that YouTube might select a different caption track language for a particular user based on the user's individual language preferences and the availability of caption tracks.
iv_load_policy
Setting the parameter's value to
1
causes video annotations to be shown by default, whereas setting to 3
causes video annotations to not be shown by default. The default value is 1
.list
The
list
parameter, in conjunction with the listType
parameter, identifies the content that will load in the player.
If the listType
parameter value is user_uploads
, then the list
parameter value identifies the YouTube channel whose uploaded videos will be loaded.
If the listType
parameter value is playlist
, then the list
parameter value specifies a YouTube playlist ID. In the parameter value, you need to prepend the playlist ID with the letters PL
as shown in the example below.
https://www.youtube.com/embed?
listType=playlist
&list=PLC77007E23FF423C6
If the
listType
parameter value is search
, then the list
parameter value specifies the search query. Note: This functionality is deprecated and will no longer be supported as of 15 November 2020.Note: If you specify values for the
list
and listType
parameters, the IFrame embed URL does not need to specify a video ID.listType
The
listType
parameter, in conjunction with the list
parameter, identifies the content that will load in the player. Valid parameter values are playlist
and user_uploads
.If you specify values for the
list
and listType
parameters, the IFrame embed URL does not need to specify a video ID.Note: A third parameter value,
search
, has been deprecated and will no longer be supported as of 15 November 2020.loop
In the case of a single video player, a setting of
1
causes the player to play the initial video again and again. In the case of a playlist
player (or custom player), the player plays the entire playlist and then starts again at the
first video.
Supported values are
0
and1
, and the default value is0
.Note: This parameter has limited
support in IFrame embeds. To loop a single video, set the
loop
parameter value to 1
and set the
playlist
parametervalue to the same video ID already specified in the Player API URL:
https://www.youtube.com/embed/VIDEO_ID?playlist=VIDEO_ID&loop=1
modestbranding
Note: This parameter is deprecated and has no effect. See the deprecation announcement for more information.
origin
This parameter provides an extra security measure for the IFrame API and is only supported for IFrame embeds. If you are using the IFrame API, which means you are setting the
enablejsapi
parameter value to 1
, you should always specify your domain as the origin
parameter value.playlist
This parameter specifies a comma-separated list of video IDs to play. If you specify a value, the first video that plays will be the
VIDEO_ID
specified in the URL path, and the videos specified in the playlist
parameter will play thereafter.playsinline
This parameter controls whether videos play inline or fullscreen on iOS. Valid values are:
0
: Results in fullscreen playback. This is currently the default value, though the default is subject to change.
1
: Results in inline playback for mobile browsers and for WebViews
created with the allowsInlineMediaPlayback
property set to YES
.
rel
Note: This parameter is changing on or after September 25, 2018.
Prior to the change, this parameter indicates whether the player should show related videos when playback of the initial video ends.
If the parameter's value is set to 1
, which is the default value, then the player does show related videos.
If the parameter's value is set to 0
, then the player does not show related videos.
After the change, you will not be able to disable related videos. Instead, if the rel
parameter is set to 0
, related videos will come from the same channel as the video that was just played.
start
This parameter causes the player to begin playing the video at the given number of seconds from the start of the video. The parameter value is a positive integer. Note that similar to the seekTo
function, the player will look for the closest keyframe to the time you specify. This means that sometimes the play head may seek to just before the requested time, usually no more than around two seconds.
widget_referrer
This parameter identifies the URL where the player is embedded. This value is used in YouTube Analytics reporting when the YouTube player is embedded in a widget, and that widget is then embedded in a web page or application. In that scenario, the origin
parameter identifies the widget provider's domain, but YouTube Analytics should not identify the widget provider as the actual traffic source. Instead, YouTube Analytics uses the widget_referrer
parameter value to identify the domain associated with the traffic source.
Revision history
August 15, 2023
Note: This is a deprecation announcement for the
modestbranding
parameter.
The modestbranding
parameter is deprecated and will
have no effect. To align with YouTube's branding requirements, the player now determines the
appropriate branding treatment based on a combination of factors, including player size, other API
parameters (e.g. controls
), and additional signals.
April 27, 2021
The playsinline
parameter definition has been updated
slightly to explain that the parameter pertains to both mobile browsers on iOS and iOS
WebViews.
October 15, 2020
The autoplay
parameter definition has been updated to
explain that if you enable Autoplay, playback will occur without any user interaction with the
player; playback data collection and sharing will therefore occur upon page load.
October 13, 2020
Note: This is a deprecation announcement for the embedded player
functionality that lets you configure the player to load search results. This announcement affects
the player's list
and
listType
parameters.
This change will become effective on or after 15 November 2020. After that time,
setting the listType
parameter value to search
will generate a
4xx
response code, such as 404
(Not Found
) or
410
(Gone
).
As an alternative, you can use the YouTube Data API's
search.list
method to retrieve search
results and then load selected videos in the player.
In addition to the change described above, the showinfo
parameter, which was
deprecated in 2018, has been removed from this document.
August 23, 2018
Note: This is a deprecation announcement for the showinfo
parameter. In addition, the behavior for the rel
parameter is changing. Titles, channel information, and related videos are an important part of YouTube’s core user experience, and these changes help to make the YouTube viewing experience consistent across different platforms.
The behavior for the
rel
parameter is changing on or after September 25, 2018. The effect of the change is that you will not be able to disable related videos. However, you will have the option of specifying that the related videos shown in the player should be from the same channel as the video that was just played.To be more specific:
Prior to the change, if the parameter's value is set to
0
, then the player does not show related videos.After the change, if the
rel
parameter is set to 0
, the player will show related videos that are from the same channel as the video that was just played.The
showinfo
parameter, which indicates whether the player should display information like the video title and uploader before the video starts playing, is also being deprecated. Following the change, the channel avatar and video title will always display before playback begins, when playback is paused, and when playback ends. The avatar being displayed is new behavior that will be consistent across all embedded players.These changes will become effective on or after September 25, 2018. After that time, the showinfo
parameter will be ignored. The behavior following the changes is consistent with the current default behavior for embedded players with the exception of the channel avatar changes mentioned above.
August 16, 2018
The cc_lang_pref
parameter can be used to specify the default language that the player will use to display captions. This parameter can be used in conjunction with the cc_load_policy
parameter to automatically show captions in the specified language during playback.
September 15, 2017
The controls
parameter's definition has been updated to remove references to the deprecated Flash (AS3) player. The value 2
has been deprecated as it was originally designed to provide a performance improvement for embeds that loaded a Flash player.
June 12, 2017
The new widget_referrer
parameter helps to enable more accurate YouTube Analytics reporting when the YouTube player is embedded in a widget, and that widget is then embedded in a web page or application.
November 1, 2016
This document has been updated to remove references to the deprecated Flash (AS3) player as well as to parameters only supported by that player. The YouTube Flash player was deprecated in January 2015.
October 20, 2016
This update contains the following changes:
The disablekb
parameter definition has been corrected to note that the default value is 0
, which means that keyboard controls are enabled. A value of 1
indicates that keyboard controls should be disabled.
The list of keyboard controls that the player supports has also been updated to include the following:
[f]: Toggle full-screen display
[j]: Jump back 10 seconds in the current video
[k]: Play / Pause
[l]: Jump ahead 10 seconds in the current video
[m]: Mute or unmute the video
[0-9]: Jump to a point in the video.
0
jumps to the beginning of the video, 1
jumps to the time 10% into the video, 2
jumps to the point 20% into the video, and so forth.In addition, the effect of pressing the [arrow left] or [arrow right] keys has changed. These keys now jump 5 seconds back (arrow left) or ahead (arrow right) in the current video.
August 11, 2016
This update contains the following changes:
The newly published YouTube API Services Terms of Service ("the Updated Terms"), discussed in detail on the YouTube Engineering and Developers Blog, provides a rich set of updates to the current Terms of Service. In addition to the Updated Terms, which will go into effect as of February 10, 2017, this update includes several supporting documents to help explain the policies that developers must follow.
The full set of new documents is described in the revision history for the Updated Terms. In addition, future changes to the Updated Terms or to those supporting documents will also be explained in that revision history. You can subscribe to an RSS feed listing changes in that revision history from a link in that document.
December 18, 2015
European Union (EU) laws require that certain disclosures must be given to and consents obtained from end users in the EU. Therefore, for end users in the European Union, you must comply with the EU User Consent Policy. We have added a notice of this requirement in our YouTube API Terms of Service.
August 19, 2015
The autohide
parameter has been deprecated for the HTML5 player. In HTML5 players, the video progress bar and player controls display or hide automatically. That behavior corresponds to an autohide
setting of 1
.
The theme
parameter has been deprecated for the HTML5 player. HTML5 players now always use the dark theme.
March 9, 2015
The document has been updated to reflect the fact that YouTube <object>
embeds, the YouTube Flash Player API and the YouTube JavaScript Player API have all been deprecated as of January 27, 2015. A deprecation warning appears in several sections of this document to help point readers to the IFrame Player API as an alternative.
The definition of the autohide
parameter has been updated to clarify the meaning of the parameter's values. The default behavior (autohide=2
) is that if the player has a 16:9 or 4:3 aspect ratio, the player's video progress bar and player controls display or hide automatically. Otherwise, those controls are visible throughout the video.
The definition of the hl
parameter has been updated to note that the parameter value could be an ISO 639-1 two-letter language code or a fully specified locale. For example, fr
and fr-ca
are both valid parameter values.
The definition of the enablejsapi
parameter has been reworded to clarify that the parameter enables a player to be controlled via API calls. The API could be either the IFrame Player API or the JavaScript Player API.
October 14, 2014
The HTML5 player now supports the cc_load_policy
, disablekb
, end
, fs
, list
, and listType
parameters. The parameter descriptions have been updated accordingly.
July 18, 2014
The new hl
parameter can be used to set the player's interface language. The interface language is used for tooltips in the player and also affects the default caption track. The selected caption track may also depend on the availability of caption tracks and user's individual language preferences.
The parameter's value is an ISO 639-1 two-letter language code, though other language input codes, such as IETF language tags (BCP 47) may also be handled properly.
The definition of the playsinline
parameter, which only affects HTML5 players on iOS, has been modified slightly. The definition now notes that setting the parameter value to 1
causes inline playback only for UIWebViews
created with the allowsInlineMediaPlayback
property set to TRUE
.
January 28, 2014
The playsinline
parameter controls whether videos play inline or fullscreen in an HTML5 player on iOS. Setting the value to 1
causes inline playback.
The Selecting content to play section has been updated to explain how to find YouTube video IDs and playlist IDs using the YouTube Data API (v3) rather than the older API version.
The controls
parameter's definition has been updated to reflect the fact that the parameter value only affects the time that the Flash player actually loads in IFrame embeds. In addition, for IFrame embeds, the parameter value also determines when the controls display in the player. If you set the parameter's value to 2
, then the controls display and the Flash player loads after the user initiates the video playback.
May 10, 2013
This update contains the following changes:
YouTube no longer identifies experimental API features and services. Instead, we now provide a list of YouTube APIs that are subject to the deprecation policy.
July 20, 2012
This update contains the following changes:
The definition of the controls
parameter has been updated to reflect support for a parameter value of 2
and to explain that, for AS3 players, the parameter value determines the time when the Flash player actually loads. If the controls
parameter is set to 0
or 1
, the Flash player loads immediately. If the parameter value is 2
, the Flash player does not load until the video playback is initiated.
June 5, 2012
This update contains the following changes:
The HTML5 player now supports the color
, modestbranding
, and rel
parameters, and the definitions for these parameters have been updated accordingly.
The definition of the showinfo
parameter has been updated to explain how that if the player is loading a playlist, and you explicitly set the parameter value to 1
, then, upon loading, the player will also display thumbnail images for the videos in the playlist. Note that this functionality is only supported for the AS3 player since that is the only player that can load a playlist.
May 4, 2012
This update contains the following changes:
The showinfo
parameter's definition has been updated to reflect the fact that the HTML5 player supports this parameter.
May 3, 2012
This update contains the following changes:
The new end
parameter specifies the time, measured in seconds from the start of the video, when the player should stop playing a video. Note that the time when playback is stopped is measured from the beginning of the video and not from the value of either the start
player parameter or the startSeconds
parameter, which is used in YouTube Player API functions for loading or queueing a video.
March 29, 2012
This update contains the following changes:
The new Embedding a YouTube player section explains different ways to embed a YouTube player in your application. This section covers manual IFrame embeds, IFrame embeds that use the IFrame Player API, and AS3 and AS2 object embeds. This section incorporates information from the old Example usage section, which has been removed.
The new Selecting content to play section explains how to configure the player to load a video, a playlist, search results for a specified query, or uploaded videos for a specified user.
The new list
and listType
parameters let you specify the content that the player should load. You can specify a playlist, a search query, or a particular user's uploaded videos.
The definitions of the fs
and rel
parameters have been updated to more clearly explain the default parameter values for the AS3 player.
The border
, color1
, egm
, hd
, and showsearch
parameters, which are all only supported for the deprecated AS2 Player API, have been moved to a new section named deprecated parameters only used in the AS2 Player API.
The document no longer provides a way to filter the parameter list to only display parameters supported in either the AS3, AS2, or HTML5 player. Instead, each parameter definition has been updated to identify the players that support that parameter.
August 11, 2011
This update contains the following changes:
The new theme
and color
parameters let you customize the appearance of the embedded player's player controls. See the YouTube API blog for more information.
June 8, 2011
This update contains the following changes:
The new modestbranding
parameter lets you use a YouTube player that does not show a YouTube logo. As of this release, the parameter was only supported for the AS3 embedded player and for IFrame embeds that loaded the AS3 player. As of June 5, 2012, the HTML5 player also supported this parameter.
February 14, 2011
This update contains the following changes:
The documentation has been updated to note that the AS2 Player API has been deprecated. The deprecation of the AS2 Player API was actually announced on October 14, 2009.
February 3, 2011
This update contains the following changes:
The documentation has been updated to identify parameters supported in the HTML5 (IFrame) embedded player.
The document now displays all of the parameters supported in any of YouTube's embedded players (HTML5, AS3, AS2).
The following parameters are supported in the AS2 player but have been deprecated for the newer AS3 and HTML5 players: border
, color1
, color2
, egm
, hd
, and showsearch
.
In addition, the loop
parameter has limited support in the AS3 player and in IFrame embeds, which could load either an AS3 or HTML player. Currently, the loop
parameter only works in the AS3 player when used in conjunction with the playlist
parameter. To loop a single video, set the loop
parameter to 1
and set the playlist
parameter value to the same video ID already specified in the Player API URL:
https://www.youtube.com/v/VIDEO_ID?version=3&loop=1&playlist=VIDEO_ID
Similarly, the controls
and playlist
parameters are supported in the AS3 and HTML5 players but are not and will not be supported in the AS2 player.
As noted above, IFrame embeds can load either an AS3 or HTML5 player. Though some parameters are not supported in both players, an IFrame embed that loads the AS3 player will support all parameters that work with that player and ignore all other parameters. Similarly, an IFrame embed that loads the HTML5 player will support all parameters that work with that player while ignoring all other parameters.
The parameter list has been updated to include the autohide
parameter, which indicates whether the player's video controls will automatically hide after a video begins playing.
The parameter list has been updated to include the controls
parameter, which indicates whether the player's video controls will display at all. (Player controls do display by default.)
The parameter list has been updated to include the playlist
parameter, which specifies a comma-separated list of video IDs to play.
The definition of the fs
has been updated to note that the fullscreen option will not work if you load the YouTube player into another SWF.
The example at the end of the document has been updated to use the embedded AS3 player instead of the embedded AS2 player. The parameters used in the example have also been updated to only include parameters that the AS3 player supports.
In addition, the instructions for constructing the URLs that contain player parameters have been updated to reflect the URL formats used by the AS3 and AS2 embedded and chromeless players as well as by the HTML5 player.
... <看更多>
originally用法 在 [請益] 原本打算做,但後來沒有的用法(want) - 看板Eng-Class 的美食出口停車場
原句:
I actually wanted to do that originally
but it seem that I ..... .
---------------------------------------------------------
主要想詢問第一個句子的 wanted,
我個人會寫成
I actually want to do something in some way, but ...it seems that...
或者
Actually, I want/ try /prefer to do something in some way,
but...it seems that....
覺得原句使用wanted有點怪怪的,
該怎麼說呢...有點饒口?
想要詢問哪一種才是正確的(或者兩個都不是?Q__Q)
以及這樣使用的原因,
因為我現在只覺得怪怪的,但沒有任何原因,
而且想不起來這種句型叫什麼,有點難google
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.39.41.143
※ 文章網址: https://www.ptt.cc/bbs/Eng-Class/M.1469686564.A.397.html
※ 編輯: Tevety (114.39.41.143), 07/28/2016 14:20:18
感謝d大,超級詳細!!
其實語感真的不準 Q Q
我只是衝著自己平常都有在聽和閱讀英文,所以常仗著語感...
有時會出現幻覺,明明沒有看過這種用法,卻出現某種直覺的壞習慣 >_<
再次感謝D大,與回文的大家!
※ 編輯: Tevety (114.39.41.143), 07/29/2016 00:04:34
... <看更多>