amp-link-rewriter
說明
允許發布商根據可設定的模式重寫網址
必要指令碼
<script async custom-element="amp-link-rewriter" src="https://cdn.ampproject.org/v0/amp-link-rewriter-0.1.js"></script>
支援的版面配置
用法
amp-link-rewriter
允許發布商根據可設定的模式重寫網址。
必要元素結構
新增必要 script
在您的 AMP 頁面的 <head>...</head>
區段中,將以下程式碼插入在 <script async src="https://cdn.ampproject.org/v0.js"></script>
行之前。
<script async custom-element="amp-link-rewriter" src="https://cdn.ampproject.org/v0/amp-link-rewriter-0.1.js" ></script>
新增 amp-link-rewriter
組件
在您的 AMP 頁面的 <body>...</body>
區段中,插入如下範例所示的程式碼 (必須針對每個供應商的使用案例進行自訂)
<amp-link-rewriter layout="nodisplay"> <script type="application/json"> { "output": "https://visit.foo.net?pid=110&url=${href}&cid=${customerId}", "section": ["#product-listing-1", "#product-listing-2"], "attribute": { "href": "((?!(https:\/\/skip\.com)).)*", "id": "comments", "class": "sidebar", "rel": "(?!(skip))*" }, "vars": { "customerId": "12345" } } </script> </amp-link-rewriter>
完整範例
最終程式碼應如下所示
<!DOCTYPE html> <html ⚡ lang="en"> <head> ... <script async custom-element="amp-link-rewriter" src="https://cdn.ampproject.org/v0/amp-link-rewriter-0.1.js" ></script> ... <script async src="https://cdn.ampproject.org/v0.js"></script> </head> <body> ... <amp-link-rewriter layout="nodisplay"> <script type="application/json"> { "output": "https://visit.foo.net?pid=110&url=${href}&cid=${customerId}", "section": ["#product-listing-1", "#product-listing-2"], "attribute": { "href": "`((?!(https:\/\/skip\.com)).)*`", "id": "comments", "class": "sidebar", "rel": "(?!(skip))*" }, "vars": { "customerId": "12345" } } </script> </amp-link-rewriter> .... </body> </html>
JSON 設定
output
(必要)
output
屬性必須是字串值,其中包含重新導向網址,以及將使用組態 JSON vars
屬性或錨點本身定義的值替換的預留位置查詢字串。
<amp-link-rewriter layout="nodisplay"> <script type="application/json"> { "output": "https://visit.foo.net?pid=110&cid=${customerId}`", "vars": { "customerId": "12345" } } </script> </amp-link-rewriter>
我們也可以在錨點資料屬性中定義資料值,如下例所示
<a href="https://www.amazon.de/s?i=computers&rh=n%3A340843031&ref=sn_gfs_co_computervs_AM_5" data-vars-event-id="231" ></a>
組態可能如下所示
{ "output": "https://visit.foo.net?eid=${eventId}&cid=${customerId}" }
重新寫入後產生的網址將會是
`https://visit.foo.net?eid=231&cid=12345`
除了在 JSON 組態的 vars
屬性中定義的變數,或作為資料屬性之外,還有其他預先定義的 vars
,它們是 AMP 網址巨集以及可用於替換輸出模式中值的錨點屬性 id
和 href
。可用的 AMP 網址巨集為 AMP_GEO
、DOCUMENT_REFERRER
和 SOURCE_URL
。下表顯示了定義的資料與預留位置之間的關係。
值 | 來源 | 範例 | 預留位置 |
---|---|---|---|
國家/地區 | 地理位置 | 美國 |
AMP_GEO(ISOCountry) |
data-vars-* | 錨點 | <a href="..." data-vars-merchant-id="123" /> |
${merchantId} |
href | 錨點 | <a href="https://retailer.com" /> |
${href} |
id | 錨點 | <a href="..." id="link" /> |
${id} |
位置 | 頁面 | https://www.partnerweb.com/ |
SOURCE_URL |
隨機 | 頁面 | Math.random().toString(32).substr(2) |
${random} |
參照網址 | 頁面 | https://google.de/ |
DOCUMENT_REFERRER |
rel | 錨點 | <a href="..." rel="pass" /> |
${rel} |
rev | 錨點 | <a href="..." rev="author" /> |
${rev} |
vars.* | 組態 | { "vars": { "publisherId": "123" } } |
${publisherId} |
section (選填)
section
屬性定義 CSS 選取器運算式陣列,用於篩選應執行網址重寫的區域。
{ "output": "https://visit.foo.net?pid=110&url=${href}&cid=${customerId}", "section": ["#product-listing-1", "#product-listing-2"] }
在先前的範例中,以屬性 ID 等於 product-listing-1
和 product-listing-2
定義的 HTML 區段將被納入網址重寫的考量。
attribute (選填)
attribute
屬性定義規則清單,用於比對從篩選區段擷取的錨點元素。這些規則是根據與 HTML 元素屬性 (例如 id
、href
、class
或 rel
) 相關聯的規則運算式所建立。
{ "section": ["#product-listing-1"], "attribute": { "href": "((?!(https://skip.com)).)*", "class": "comments" } }
位於 ID 為 product-listing-1
的 HTML 區域內的錨點必須符合為 href 和 class 屬性定義的規則運算式。規則運算式將以 ^
和 \$
包裝,以進行完整比對。
在此範例中,這表示所有包含 youtube.com
和 mobile.vodafone.de
的錨點都將被排除。此外,包含的錨點需要具有 class 屬性,且值為 comments
。
scopeDocument (選填)
預設情況下,如果頁面沒有任何連結符合組態中指定的 attribute
和 section
範圍,則所有錨點都將被重寫。
若要變更此行為,您需要將 scopeDocument
設定為 false
。
驗證
請參閱 AMP 驗證器規格中的 amp-link-rewriter
規則。
您已閱讀這份文件十幾次,但它仍然沒有涵蓋您的所有問題?也許其他人也有同感:請在 Stack Overflow 上與他們聯繫。
前往 Stack Overflow 發現錯誤或缺少功能?AMP 專案大力鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的長期參與者,但我們也歡迎針對您特別熱衷的問題提供一次性貢獻。
前往 GitHub