vaersaagod / toolmate
这是你口袋里的工具吗,还是你只是高兴看到我,伙计?
Requires
- php: ^8.0
- craftcms/cms: ^3.1.0|^4.0.0-beta.1|^5.0.0-beta.1
- matthiasmullie/minify: ^1.3.0
- voku/html-min: ^4.4.0
README
这是你口袋里的工具吗,还是你只是高兴看到我,伙计!
要求
此插件需要Craft CMS 3.1.0或更高版本。
安装
要安装插件,可以从插件商店安装,或者按照以下说明操作
- 通过在项目目录中运行
composer require vaersaagod/toolmate
使用composer安装。 - 在Craft控制面板的设置→插件下安装插件,或从命令行通过
./craft install/plugin toolmate
安装。
配置
可以通过在Craft配置文件夹中创建一个名为toolmate.php
的文件来配置ToolMate,并根据需要覆盖设置。
publicRoot [字符串]
默认:@webroot
设置公共webroot,在$_SERVER['DOCUMENT_ROOT']
和@webroot
不正确的服务器上使用。
enableMinify [布尔值]
默认:true
启用/禁用所有压缩。
embedCacheDuration [整数|字符串|布尔值|null]
默认:null
从craft.toolmate.getVideoEmbed()
缓存响应的秒数。
如果设置为null
,则使用核心cacheDuration
设置。
如果设置为false
,则不缓存响应。
如果设置为0
,则无限期存储缓存响应。
有关支持的值类型列表,请参阅craft\helpers\ConfigHelper::durationInSeconds()
。
embedCacheDurationOnError [整数|字符串|布尔值|null]
默认:300
(5分钟)
在出现错误时(即无法返回有效的嵌入代码)从craft.toolmate.getVideoEmbed()
缓存响应的秒数。
如果设置为null
,则使用默认值(300
,5分钟)。
如果设置为false
,则不缓存错误响应。
如果设置为0
,则无限期存储错误响应缓存。
有关支持的值类型列表,请参阅craft\helpers\ConfigHelper::durationInSeconds()
。
csp [数组|null]
默认 null
配置Toolmate设置的内容安全策略(CSP)头部。一些有用的提示
- 避免使用
unsafe-inline
和unsafe-eval
策略,特别是对于script-src
指令。CSP非ces应该理想地用于内联脚本或样式标签,请参阅cspNonce()
Twig函数。 - 非ces和
unsafe-inline
不能结合使用。Toolmate绕过此问题以避免CSP错误,但简而言之;如果您同时使用unsafe-inline
,则不需要设置非ces。 - 由
cspNonce()
生成的CSP非ces可以安全地放在{% cache %}
标签内 - 要启用data-URLs,将
data:
策略添加到相关指令 - 对于CP请求,Toolmate将始终添加必要的
unsafe-inline
和unsafe-eval
策略,因为不使用这些策略就无法使用CP。
csp[enabled] [布尔值]
默认 false
如果设置为false
,则不会为任何请求发送CSP头部。
csp[enabledForCp] [布尔值]
默认 false
如果设置为false
,则仅对站点请求发送CSP头部。
csp[reportOnly] [布尔值]
默认 false
如果设置为true
,则发送CSP头部,但不强制执行(即干运行模式)。用于测试策略非常有用。
csp[directives] [数组]
请参阅https://content-security-policy.com/,以及下面的示例配置。
示例CSP配置
'csp' => [ 'enabled' => true, 'enabledForCp' => false, 'reportOnly' => false, 'directives' => [ 'defaultSrc' => [ "'self'", ], 'scriptSrc' => [ "'self'", "'unsafe-inline'", ], // Sources for stylesheets 'styleSrc' => [ "'self'", "'unsafe-inline'", ], // Sources for images 'imgSrc' => [ "'self'", 'https://some-project.imgix.net', 'data:' ], // Sources for iframes 'frameSrc' => [ "'self'", 'https://www.youtube.com https://player.vimeo.com https://# https://# https://bid.g.doubleclick.net', ], // Domains that are allowed to iframe this site 'frameAncestors' => [ "'self'", ], 'baseUri' => [ "'none'", ], 'connectSrc' => [], 'fontSrc' => [ //"'self'", ], 'objectSrc' => [], 'mediaSrc' => [], 'sandbox' => [], 'reportUri' => [], 'childSrc' => [], 'formAction' => [], 'reportTo' => [], 'workerSrc' => [], 'manifestSrc' => [], 'navigateTo' => [], ], ],
模板变量
craft.toolmate.inline(filename [, remote=false])
{{ craft.toolmate.inline('/assets/critical.css') }}
craft.toolmate.stamp(filename [, mode = 'file', type = 'ts'])
{# /assets/bundle.1522425799.js #}
{{ craft.tool.stamp('/assets/bundle.js') }}
{# /assets/1522425799/bundle.js #}
{{ craft.tool.stamp('/assets/bundle.js', 'folder') }}
{# /assets/5140247221/bundle.js #}
{{ craft.tool.stamp('/assets/bundle.js', 'folder', 'hash') }}
{# /assets/bundle.js?ts=1522425799 #}
{{ craft.tool.stamp('/assets/bundle.js', 'query') }}
{# 1522425799 #}
{{ craft.tool.stamp('/assets/bundle.js', 'tsonly') }}
craft.toolmate.setCookie(params [, secure = false])
{% do craft.toolmate.setCookie({ name: 'testing', value: 'Just testing!' }) %}
{% do craft.toolmate.setCookie({ name: 'testingsecure', value: { lorem: 'ipsum', dolor: 'sit amet' } }, true) %}
{% set params = {
name: 'cookiename',
value: 'thevalue',
expire: 0,
path: '/',
domain: '',
secure: false,
httpOnly: false,
sameSite: null,
} %}
{% do craft.toolmate.setCookie(params) %}
craft.toolmate.getCookie(name [, secure = false])
{{ craft.toolmate.getCookie('testing') }}
{{ dump(craft.toolmate.getCookie('testingsecure', true)) }}
craft.toolmate.getVideoEmbed(url [, params = []])
{% set videoEmbed = craft.toolmate.getVideoEmbed(videoUrl, {
youtube_enablejsapi: 1,
youtube_rel: 0,
youtube_showinfo: 0,
youtube_controls: 1,
youtube_autoplay: 0,
youtube_modestbranding: 1,
youtube_playsinline: 0,
vimeo_byline: 0,
vimeo_title: 0,
vimeo_autoplay: 0,
vimeo_portrait: 0
}) %}
Twig 标签
压缩
<style>
{% minify css %}
.lorem {
width: 200px;
}
.ipsum {
padding: 0px;
}
{% endminify %}
</style>
<script>
{% minify js %}
var myFunction = function () {
console.log('Some inline JS');
}
{% endminify %}
</script>
{% minify html %}
<div>
<p>Some html</p>
</div>
{% endminify %}
Twig 函数
inline(filename [, remote=false])
参见 craft.toolmate.inline
.
stamp(filename [, mode = 'file', type = 'ts'])
参见 craft.toolmate.stamp
.
setCookie(params [, secure = false])
参见 craft.toolmate.setCookie
.
getCookie(name [, secure = false])
参见 craft.toolmate.getCookie
.
getVideoEmbed(url [, params = []])
参见 craft.toolmate.getCookie
.
cspNonce(directive, [, asAttribute = false, hash = true])
输出 CSP 随机数。示例
<script nonce="{{ cspNonce('script-src') }}"> console.log('Hello world'); </script>
<style nonce="{{ cspNonce('style-src') }}"> body { ... } </style>
{% js 'foo.js' with { nonce: cspNonce('script-src') } %}
价格、许可和支持
该插件遵循 MIT 许可证发布。它是为 Værsågod 和其朋友们制作的,不提供支持。提交的问题如果解决了某个问题,则会被解决。
变更日志
参见 CHANGELOG.MD.
鸣谢
由 Værsågod 提供
图标由 Freepik from Flaticon 设计。