Skip to content

Commit

Permalink
更新rightmenu
Browse files Browse the repository at this point in the history
  • Loading branch information
xlenco committed Aug 22, 2022
1 parent e24ac7d commit 8546d0b
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 57 deletions.
55 changes: 55 additions & 0 deletions source/css/_custom/iconfont.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
@font-face {
font-family: "iconfont"; /* Project id 3498204 */
src: url('https://ik.imagekit.io/nicexl/icon/iconfont.woff2?t=1659311976990') format('woff2'),
url('https://ik.imagekit.io/nicexl/icon/iconfont.woff?t=1659311976990') format('woff'),
url('https://ik.imagekit.io/nicexl/icon/iconfont.ttf?t=1659311976990') format('truetype');
}

.iconfont {
font-family: "iconfont" !important;
font-size: 16px;
font-style: normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

.icon-danmu:before {
content: "\e612";
}

.icon-biaoqian:before {
content: "\e63d";
}

.icon-fenlei:before {
content: "\e601";
}

.icon-fanti:before {
content: "\e6d8";
}

.icon-baidu:before {
content: "\e60a";
}

.icon-csdn:before {
content: "\e600";
}

.icon-RSS:before {
content: "\e78a";
}

.icon-bilibili:before {
content: "\e609";
}

.icon-gitee:before {
content: "\e60c";
}

.icon-zhihu:before {
content: "\e60d";
}

223 changes: 179 additions & 44 deletions source/js/rightmenu.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
let kk = {};
kk.showRightMenu = function(isTrue, x=0, y=0){
console.log(
"Codes uses GPL Licence"
)

function insertAtCursor(myField, myValue) {

//IE 浏览器
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
sel.select();
}

//FireFox、Chrome等
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;

// 保存滚动条
var restoreTop = myField.scrollTop;
myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);

if (restoreTop > 0) {
myField.scrollTop = restoreTop;
}

myField.focus();
myField.selectionStart = startPos + myValue.length;
myField.selectionEnd = startPos + myValue.length;
} else {
myField.value += myValue;
myField.focus();
}
}
let rmf = {};
rmf.showRightMenu = function (isTrue, x = 0, y = 0) {
let $rightMenu = $('#rightMenu');
$rightMenu.css('top',x+'px').css('left',y+'px');
$rightMenu.css('top', x + 'px').css('left', y + 'px');

if(isTrue){
if (isTrue) {
$rightMenu.show();
}else{
} else {
$rightMenu.hide();
}
}
kk.switchDarkMode = function(){
rmf.switchDarkMode = function () {
const nowMode = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'light'
if (nowMode === 'light') {
activateDarkMode()
Expand All @@ -25,82 +60,182 @@ kk.switchDarkMode = function(){
typeof FB === 'object' && window.loadFBComment()
window.DISQUS && document.getElementById('disqus_thread').children.length && setTimeout(() => window.disqusReset(), 200)
};
kk.switchReadMode = function(){
rmf.copyWordsLink = function () {
let url = window.location.href
let txa = document.createElement("textarea");
txa.value = url;
document.body.appendChild(txa)
txa.select();
document.execCommand("Copy");
document.body.removeChild(txa);
Swal.fire("复制成功!");
}
rmf.switchReadMode = function () {
const $body = document.body
$body.classList.add('read-mode')
const newEle = document.createElement('button')
newEle.type = 'button'
newEle.className = 'fas fa-sign-out-alt exit-readmode'
$body.appendChild(newEle)

function clickFn () {
function clickFn() {
$body.classList.remove('read-mode')
newEle.remove()
newEle.removeEventListener('click', clickFn)
}

newEle.addEventListener('click', clickFn)
}
kk.switchTheme=function(load=false){
//空字符串表示butterfly原版主题(即不加载css)
//FallGuys.css是我自己的魔改主题,需替换
let themes = ['',''];
let vTheme = parseInt(localStorage.getItem('visitor-theme'));
if(!vTheme){
vTheme = load?0:1;
}else{
vTheme += load?0:1;
vTheme%=themes.length;
}
localStorage.setItem('visitor-theme',vTheme)
let themesrc = ''
if(themes[vTheme]){
themesrc += window.location.origin+'/css/dorakika/'+themes[vTheme];
}
//css引入时link标签添加属性tag="theme"
let themeLink = $(document).find('[tag="theme"]')[0];
if(themeLink)themeLink.href = themesrc;
};

//复制选中文字
kk.copySelect = function(){
document.execCommand('Copy',false,null);
btf.snackbarShow('已复制!!');
rmf.copySelect = function () {
document.execCommand('Copy', false, null);
//这里可以写点东西提示一下 已复制
}

//回到顶部
kk.scrollToTop = function(){
rmf.scrollToTop = function () {
btf.scrollToDest(0, 500);
}
rmf.translate = function () {
document.getElementById("translateLink").click();
}

// 右键菜单事件
if(! (navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))){
window.oncontextmenu = function(event){
document.onkeydown = function (event) {
event = (event || window.event);
if (event.keyCode == 17) {
console.log("你知道的太多了");
return;
}
}

function popupMenu() {
//window.oncontextmenu=function(){return false;}
window.oncontextmenu = function (event) {
console.log(event.keyCode)
$('.rightMenu-group.hide').hide();
if(document.getSelection().toString()){
//如果有文字选中,则显示 文字选中相关的菜单项
if (document.getSelection().toString()) {
$('#menu-text').show();
}

console.log(event.target);
if (document.getElementById('post')) {
$('#menu-post').show();
} else {
if (document.getElementById('page')) {
$('#menu-post').show();
}
}
var el = window.document.body;
el = event.target;
var a=/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$/
if (a.test(window.getSelection().toString())){
$('#menu-too').show()
}
if (el.tagName == 'A') {
$('#menu-to').show()
rmf.open = function () {
location.href = el.href
}
rmf.openWithNewTab = function () {
window.open(el.href);
window.location.reload();
}
rmf.copyLink = function () {
let url = el.href
let txa = document.createElement("textarea");
txa.value = url;
document.body.appendChild(txa)
txa.select();
document.execCommand("Copy");
document.body.removeChild(txa);
}
}
if (el.tagName == 'IMG') {
$('#menu-img').show()
rmf.openWithNewTab = function () {
window.open(el.src);
window.location.reload();
}
rmf.click = function () {
el.click()
}
rmf.copyLink = function () {
let url = el.src
let txa = document.createElement("textarea");
txa.value = url;
document.body.appendChild(txa)
txa.select();
document.execCommand("Copy");
document.body.removeChild(txa);
}
} else if (el.tagName == "TEXTAREA" || el.tagName == "INPUT") {
$('#menu-paste').show();
rmf.paste = function () {
navigator.permissions
.query({
name: 'clipboard-read'
})
.then(result => {
if (result.state == 'granted' || result.state == 'prompt') {
//读取剪贴板
navigator.clipboard.readText().then(text => {
console.log(text)
insertAtCursor(el, text)
})
} else {
alert('请允许读取剪贴板!')
}
})
}
}
let pageX = event.clientX + 10;
let pageY = event.clientY;
let rmWidth = $('#rightMenu').width();
let rmHeight = $('#rightMenu').height();
if(pageX + rmWidth > window.innerWidth){
pageX -= rmWidth+10;
if (pageX + rmWidth > window.innerWidth) {
pageX -= rmWidth + 10;
}
if(pageY + rmHeight > window.innerHeight){
if (pageY + rmHeight > window.innerHeight) {
pageY -= pageY + rmHeight - window.innerHeight;
}



kk.showRightMenu(true, pageY, pageX);
rmf.showRightMenu(true, pageY, pageX);
return false;
};

window.addEventListener('click',function(){kk.showRightMenu(false);});
// window.addEventListener('load',function(){kk.switchTheme(true);});
window.addEventListener('click', function () {
rmf.showRightMenu(false);
});
}
if (!(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
popupMenu()
}
const box = document.documentElement

function addLongtabListener(target, callback) {
let timer = 0 // 初始化timer

target.ontouchstart = () => {
timer = 0 // 重置timer
timer = setTimeout(() => {
callback();
timer = 0
}, 380) // 超时器能成功执行,说明是长按
}

target.ontouchmove = () => {
clearTimeout(timer) // 如果来到这里,说明是滑动
timer = 0
}

target.ontouchend = () => { // 到这里如果timer有值,说明此触摸时间不足380ms,是点击
if (timer) {
clearTimeout(timer)
}
}
}
let rm={};rm.stopdragimg=$("img"),rm.stopdragimg.on("dragstart",(function(){return!1})),rm.showRightMenu=function(e,t=0,n=0){let o=$("#rightMenu");o.css("top",t+"px").css("left",n+"px"),e?(o.show(),stopMaskScroll()):o.hide()},rm.hideRightMenu=function(){rm.showRightMenu(!1),$("#rightmenu-mask").attr("style","display: none")};let rmWidth=$("#rightMenu").width(),rmHeight=$("#rightMenu").height();rm.reloadrmSize=function(){rmWidth=$("#rightMenu").width(),rmHeight=$("#rightMenu").height()};let domhref="",domImgSrc="";function downloadImage(e,t){rm.hideRightMenu(),0==rm.downloadimging?(rm.downloadimging=!0,btf.snackbarShow("正在下载中,请稍后",!1,1e4),setTimeout((function(){let n=new Image;n.setAttribute("crossOrigin","anonymous"),n.onload=function(){let e=document.createElement("canvas");e.width=n.width,e.height=n.height,e.getContext("2d").drawImage(n,0,0,n.width,n.height);let o=e.toDataURL("image/png"),i=document.createElement("a"),r=new MouseEvent("click");i.download=t||"photo",i.href=o,i.dispatchEvent(r)},n.src=e,btf.snackbarShow("图片已添加盲水印,请遵守版权协议"),rm.downloadimging=!1}),"10000")):btf.snackbarShow("有正在进行中的下载,请稍后再试")}function imageToBlob(e){const t=new Image,n=document.createElement("canvas"),o=n.getContext("2d");return t.crossOrigin="",t.src=e,new Promise((e=>{t.onload=function(){n.width=this.naturalWidth,n.height=this.naturalHeight,o.drawImage(this,0,0),n.toBlob((t=>{e(t)}),"image/png",.75)}}))}async function copyImage(e){const t=await imageToBlob(e),n=new ClipboardItem({"image/png":t});navigator.clipboard.write([n])}function stopMaskScroll(){if(document.getElementById("rightmenu-mask")){document.getElementById("rightmenu-mask").addEventListener("mousewheel",(function(e){rm.hideRightMenu()}),!1)}if(document.getElementById("rightMenu")){document.getElementById("rightMenu").addEventListener("mousewheel",(function(e){rm.hideRightMenu()}),!1)}}window.oncontextmenu=function(e){if(document.body.clientWidth>768){let t=e.clientX+10,n=e.clientY,o=$(".rightMenuOther"),i=$(".rightMenuPlugin"),r=$("#menu-copytext"),c=$("#menu-commenttext"),m=$("#menu-newwindow"),a=$("#menu-copylink"),d=$("#menu-copyimg"),h=$("#menu-downloadimg"),u=$("#menu-search"),l=$("#menu-searchBaidu"),g=e.target.href,s=e.target.currentSrc,w=!1;return o.show(),selectTextNow&&window.getSelection()?(w=!0,r.show(),c.show(),u.show(),l.show(),o.hide()):(r.hide(),c.hide(),l.hide(),u.hide()),g?(w=!0,m.show(),a.show(),o.hide(),domhref=g):(m.hide(),a.hide()),s?(w=!0,d.show(),h.show(),o.hide(),domImgSrc=s):(d.hide(),h.hide()),w?i.show():i.hide(),rm.reloadrmSize(),t+rmWidth>window.innerWidth&&(t-=rmWidth+10),n+rmHeight>window.innerHeight&&(n-=n+rmHeight-window.innerHeight),rm.showRightMenu(!0,n,t),$("#rightmenu-mask").attr("style","display: flex"),!1}},rm.downloadimging=!1,rm.writeClipImg=function(e){console.log("按下复制"),rm.hideRightMenu(),btf.snackbarShow("正在下载中,请稍后",!1,1e4),0==rm.downloadimging&&(rm.downloadimging=!0,setTimeout((function(){copyImage(e),btf.snackbarShow("复制成功!图片已添加盲水印,请遵守版权协议"),rm.downloadimging=!1}),"10000"))},rm.copyUrl=function(e){$("body").after("<input id='copyVal'></input>");var t=e,n=document.getElementById("copyVal");n.value=t,n.select(),n.setSelectionRange(0,n.value.length),document.execCommand("copy"),$("#copyVal").remove()},rm.rightmenuCopyText=function(e){navigator.clipboard&&navigator.clipboard.writeText(e),rm.hideRightMenu()},rm.copyPageUrl=function(){var e=window.location.href;rm.copyUrl(e),btf.snackbarShow("复制本页链接地址成功",!1,2e3),rm.hideRightMenu()};var selectTextNow="",selectTextPre="";function selceText(){var e;(e=document.selection?document.selection.createRange().text:window.getSelection()+"")?(selectTextNow=e,selectTextPre=e):selectTextNow=""}function replaceAll(e,t,n){return e.split(t).join(n)}function addRightMenuClickEvent(){$("#menu-backward").on("click",(function(){window.history.back(),rm.hideRightMenu()})),$("#menu-forward").on("click",(function(){window.history.forward(),rm.hideRightMenu()})),$("#menu-refresh").on("click",(function(){window.location.reload()})),$("#menu-top").on("click",(function(){btf.scrollToDest(0,500),rm.hideRightMenu()})),$(".menu-link").on("click",rm.hideRightMenu),$("#menu-darkmode").on("click",rm.switchDarkMode),$("#menu-home").on("click",(function(){window.location.href=window.location.origin})),$("#rightmenu-mask").on("click",rm.hideRightMenu),$("#rightmenu-mask").contextmenu((function(){return rm.hideRightMenu(),!1})),$("#menu-copy").on("click",rm.copyPageUrl),$("#menu-copytext").on("click",(function(){rm.rightmenuCopyText(selectTextPre),btf.snackbarShow("复制成功,复制和转载请标注本文地址")})),$("#menu-commenttext").on("click",(function(){rm.rightMenuCommentText(selectTextPre)})),$("#menu-newwindow").on("click",(function(){window.open(domhref),rm.hideRightMenu()})),$("#menu-copylink").on("click",(function(){rm.rightmenuCopyText(domhref),btf.snackbarShow("已复制链接地址")})),$("#menu-downloadimg").on("click",(function(){downloadImage(domImgSrc,"blog.justlovesmile.top")})),$("#menu-copyimg").on("click",(function(){rm.writeClipImg(domImgSrc)})),$("#menu-searchBaidu").on("click",rm.searchBaidu)}document.onmouseup=document.ondbclick=selceText,rm.rightMenuCommentText=function(e){rm.hideRightMenu();var t=document.getElementsByClassName("el-textarea__inner")[0];let n=document.createEvent("HTMLEvents");n.initEvent("input",!0,!0);let o=replaceAll(e,"\n","\n> ");t.value="> "+o+"\n\n",t.dispatchEvent(n);var i=document.querySelector("#post-comment").offsetTop;window.scrollTo(0,i-80),t.focus(),t.setSelectionRange(-1,-1)},rm.searchBaidu=function(){btf.snackbarShow("即将跳转到百度搜索",!1,2e3),setTimeout((function(){window.open("https://www.baidu.com/s?wd="+selectTextPre)}),"2000"),rm.hideRightMenu()},rm.switchDarkMode=function(){rm.hideRightMenu(),"light"==("dark"===document.documentElement.getAttribute("data-theme")?"dark":"light")?(activateDarkMode(),saveToLocal.set("theme","dark",2),void 0!==GLOBAL_CONFIG.Snackbar&&btf.snackbarShow(GLOBAL_CONFIG.Snackbar.day_to_night)):(activateLightMode(),saveToLocal.set("theme","light",2),void 0!==GLOBAL_CONFIG.Snackbar&&btf.snackbarShow(GLOBAL_CONFIG.Snackbar.night_to_day)),"function"==typeof utterancesTheme&&utterancesTheme(),"object"==typeof FB&&window.loadFBComment(),window.DISQUS&&document.getElementById("disqus_thread").children.length&&setTimeout((()=>window.disqusReset()),200)},addRightMenuClickEvent();

addLongtabListener(box, popupMenu)
Loading

0 comments on commit 8546d0b

Please sign in to comment.