如何通过 url 参数更改开放图元标记内容

How to change open graph meta tag content via url params

我正在尝试使用 url 参数和开放图元标记制作自定义嵌入生成器,但每当我 post 将 link 发送到 Discord 等网站时,它都会使用我为它设置的默认标签而不是 url 参数。有谁知道如何解决这一问题?我已经 post 编辑了代码和下面发生的示例图像。

<html prefix="og: https://ogp.me/ns#">

<head>
    <meta property="og:title" content="Title">
    <meta property="og:description" content="Description">
    <script>
        const queryString = window.location.search;
        const urlParams = new URLSearchParams(queryString);

        if (urlParams.has("title")) {
            const title = urlParams.get("title");
            document.querySelector('meta[property="og:title"]').setAttribute("content", title);
        };

        if (urlParams.has("desc")) {
            const desc = urlParams.get("desc");
            document.querySelector('meta[property="og:description"]').setAttribute("content", desc);
        };
    </script>
</head>

</html>

您不能期望抓取开放图形数据的爬虫会在您的页面上执行 JS。唯一有效的解决方案是在服务器上呈现此 HTML。