Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copyright when copying #84

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ post_edit:
# Available values: left | right | false
post_navigation: left

# Add copyright when copying
post_copyright:
enable: false
maxLength: 10 # Trigger length

# ---------------------------------------------------------------
# Custom Page Settings
Expand Down
4 changes: 4 additions & 0 deletions layout/_macro/post.njk
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@
{{ partial('_partials/post/post-copyright.njk') }}
{%- endif %}

{%- if theme.post_copyright.enable %}
{{ partial('_partials/post/post-clipboard.njk') }}
{%- endif %}

{%- if theme.follow_me %}
{{ partial('_partials/post/post-followme.njk', {}, {cache: theme.cache.enable}) }}
{%- endif %}
Expand Down
11 changes: 11 additions & 0 deletions layout/_partials/post/post-clipboard.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{%- if theme.post_copyright.enable %}
<script>
document.body.oncopy = function (e) {
var selection = window.getSelection();
if (("" + selection).length < {{ theme.post_copyright.maxLength }}) return;
e.preventDefault();
let copyMsg = window.getSelection() + "作者: {{ page.author or author }} 链接: {{ page.permalink }}"
e.clipboardData.setData("Text", copyMsg);
}
</script>
{%- endif %}
12 changes: 12 additions & 0 deletions layout/_partials/post/post-copyright.njk
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@
</li>
</ul>
</div>

{%- if theme.post_copyright.enable %}
<script>
document.body.oncopy = function (e) {
var selection = window.getSelection();
if (("" + selection).length < {{ theme.post_copyright.maxLength }}) return;
e.preventDefault();
let copyMsg = window.getSelection() + "作者: {{ page.author or author }} 链接: {{ page.permalink }}"
e.clipboardData.setData("Text", copyMsg);
}
</script>
{%- endif %}