如何在自定义中允许特定的 HTML 标签

I’m trying to add the google custom search engine to the header of our discourse forum. I put in the javascript to the head tag and then in the body there is something like <gcse:search linkTarget="_self"></gcse:search> - which will show the google custom search engine.

When I put this in and save it however, it doesn’t show up on the site. I imagine it is being removed by some sort of html filter? (and I should add that by “doesn’t show up” I mean the code itself, not just that the javascript isn’t loading somehow - when I surround it with <!-- -->, the code passes through, but obviously doesn’t work).

Is there a way to get this to pass through?

Thanks!

Looking at this it apparently strips gcse - so I get:

<search linktarget=\"_self\"><\/search>

Which doesn’t load anything

Instead of <gcse:search linkTarget="_self"></gcse:search> it strips out the : and the /gcse

I tried <gcse\:search><\/gcse\:search> but then I get <gcse search> (no : ) and </gcse> (no : or search)

Is there another way to get a variable to pass through the customization editor so that I can get it to show up?

This is just a proof of concept, so you’ll have to play around with it to see how/if it can work for your needs. Try adding this to the </head> tab of a theme:

<script type="text/discourse-plugin" version="0.8.18">
    api.decorateWidget('header-contents:after', helper => {
        return helper.rawHtml(`<gcse:search linkTarget="_self"></gcse:search>`);
    });
</script>

(Edit: improved the answer a little after having more time to look at it :slightly_smiling_face:)

5 个赞

That’s awesome - Thanks! Works perfectly. Both versions seemed to work the same, but I went with your current edit.

Actually I may have spoke a little too soon… It’s fairly intermittent when it will show up. I had both versions of your script in so I thought this newer one was working (and it did once - minus putting the search bar on top of the menu, etc. - lower half of the header).

The first script appears to work on the front page only if I put in at the body close section - then it shows up at the top of the page oddly.

I’d like to try to get it right below the header bar on top on every page.

Instead of ‘header-contents:after’ - is there a ‘body-contents:before’? that I could try. I went through the api docs, but couldn’t find anything that showed such a declaration…

1 个赞

Sure, if you change it from header-contents:after to just header:after, that should do the trick. You will need to style it with CSS to get it exactly how you want it, but that should get you on track :slightly_smiling_face:

3 个赞

你好 @tshenry

能否请你帮忙提供一下你在 Discourse 中构建 GCSE 的相关代码?

谢谢

你好 @yyhmsg

能否请您协助我进行 GCSE 的实施?

很遗憾,我无法提供太多帮助,因为我不再使用 GCSE 了。我之前确实让它运行了一段时间,但后来逐渐远离了 Google 的服务,并在好几个 Discourse 版本之前将其移除了。上面发布的内容确实可行,但我不得不花不少时间调试,以解决无限滚动的问题以及应对 Discourse 的更新变化。

你尝试过哪些方法?遇到了什么错误或问题?

一个更简洁、更容易的方案,可能不需要太多维护,就是在顶部添加一个按钮,链接到 Google 托管的搜索页面,或者你自己搭建的某个搜索页面。

1 个赞

@yyhmsg

我已经实现了代码,并做了一些 CSS 修改以创建搜索框。但是,当我点击搜索选项时,它默认跳转到 Discourse 搜索,而不是 Google 搜索。我卡在了这里。

如果您已设置但搜索指向了错误的搜索工具,那么可能需要更改查询参数名称,使两者都不再使用 q=

queryParameterName 字符串 查询参数名称——例如 q(默认值)或 query。该参数将嵌入到 URL 中(例如 Example Domain

因此,您的 GCSE 必须设置为使用不同的 queryParameterName,例如 qr=。这样,当您提交搜索时,只有 GCSE 会响应,因为 Discourse 期望的是 q=

你好 @yyhmsg

我正在为 GCSE 使用 Overlay 主题。我希望在同一页面上显示搜索结果,而不是跳转到新页面,因为 Discourse 不支持双页面格式。但问题是,点击搜索按钮后,URL 仍然会附加查询字符串 “?search=search+term”。

这导致无法显示任何搜索结果,并提示“没有最新话题”。

请问我是否遗漏了什么?

提前感谢!

如果您能展示所使用的代码,并在可能的情况下提供您正在操作的网站链接,那将非常有帮助。

你好 @yyhmsg

我正在开发的网站是:mpl.co
我们能否通过 Google Meet 或其他工具进行屏幕共享?请告知您方便的时间。

谢谢。

很抱歉,我是一名医疗工作者,日程非常繁忙,只能在患者之间的短暂间隙处理这类事务。

我是否可以建议一个类似的插件——https://github.com/sylque/discpage——您可以在其中嵌入完整的页面内 GCSE 代码,然后在页眉添加一个按钮,链接到该页面。这样看起来会相当简洁……

感谢 @yyhmsg,我这边会尝试一下。谢谢您的建议。