Skip to content
Open
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
92 changes: 92 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Created by .ignore support plugin (hsz.mobi)
### Node template
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

.idea/
22 changes: 11 additions & 11 deletions Plugin.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php
/**
* 一个 用于Typecho的 复制代码 的插件</a>
*
* @package CodeCopy
*
* @package CodeCopy
* @author 兔子昂
* @version 1.0.0
* @link https://www.tuziang.com
*/
class CodeCopy_Plugin implements Typecho_Plugin_Interface {
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
Expand All @@ -22,7 +22,7 @@ public static function activate() {

/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
*
* @static
* @access public
* @return void
Expand All @@ -32,7 +32,7 @@ public static function deactivate(){}

/**
* 获取插件配置面板
*
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
Expand All @@ -44,7 +44,7 @@ public static function config(Typecho_Widget_Helper_Form $form){

/**
* 个人用户的配置面板
*
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
Expand All @@ -53,29 +53,29 @@ public static function personalConfig(Typecho_Widget_Helper_Form $form){}

/**
* 插件实现方法
*
*
* @access public
* @return void
*/
public static function render() {

}

/**
*为header添加css文件
*@return void
*/
public static function header() {

}

/**
*为footer添加js文件
*@return void
*/
public static function footer() {
$path = Helper::options()->pluginUrl . '/codecopy/';

$path = Helper::options()->pluginUrl . '/CodeCopy/';
echo '<script type="text/javascript" src="' . $path . 'codecopy.js"></script>';
}
}
75 changes: 40 additions & 35 deletions codecopy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//html5 给typecho添加 复制代码 功能
//html5 给typecho添加 复制代码 功能
// by 兔子昂
var codeblocks = document.getElementsByTagName("pre")
// fixed by xiaomiwujiecao
$(function () {
var codeblocks = document.getElementsByTagName("pre")
//循环每个pre代码块,并添加 复制代码

for (var i = 0; i < codeblocks.length; i++) {
for (var i = 0; i < codeblocks.length; i++) {
//显示 复制代码 按钮
currentCode = codeblocks[i]
currentCode.style = "position: relative;"
Expand All @@ -16,46 +18,49 @@ for (var i = 0; i < codeblocks.length; i++) {
currentCode.appendChild(copy)
//让所有 "复制"按钮 全部隐藏
copy.style.visibility = "hidden"
}
}


for (var i = 0; i < codeblocks.length; i++) {
for (var i = 0; i < codeblocks.length; i++) {


!function (i) {
//鼠标移到代码块,就显示按钮
codeblocks[i].onmouseover = function () {
codeblocks[i].childNodes[1].style.visibility = "visible"
}

//执行 复制代码 功能
function copyArticle(event) {
const range = document.createRange();

//范围是 code,不包括刚才创建的div
range.selectNode(codeblocks[i].childNodes[0]);

const selection = window.getSelection();
if (selection.rangeCount > 0) selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');

codeblocks[i].childNodes[1].innerHTML = "复制成功"
setTimeout(function () {
codeblocks[i].childNodes[1].innerHTML = "复制"
}, 1000);
//清除选择区
if (selection.rangeCount > 0) selection.removeAllRanges(); 0
}
codeblocks[i].childNodes[1].addEventListener('click', copyArticle, false);
//鼠标移到代码块,就显示按钮
codeblocks[i].onmouseover = function () {
codeblocks[i].childNodes[1].style.visibility = "visible"
}

//执行 复制代码 功能
function copyArticle(event) {
const range = document.createRange();

//范围是 code,不包括刚才创建的div
range.selectNode(codeblocks[i].childNodes[0]);

const selection = window.getSelection();
if (selection.rangeCount > 0) selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');

codeblocks[i].childNodes[1].innerHTML = "复制成功"
setTimeout(function () {
codeblocks[i].childNodes[1].innerHTML = "复制"
}, 1000);
//清除选择区
if (selection.rangeCount > 0) selection.removeAllRanges();
0
}

codeblocks[i].childNodes[1].addEventListener('click', copyArticle, false);

}(i);

!function (i) {
//鼠标从代码块移开 则不显示复制代码按钮
codeblocks[i].onmouseout = function () {
codeblocks[i].childNodes[1].style.visibility = "hidden"
}
//鼠标从代码块移开 则不显示复制代码按钮
codeblocks[i].onmouseout = function () {
codeblocks[i].childNodes[1].style.visibility = "hidden"
}
}(i);
}
}
})