Skip to content

Commit

Permalink
improve dict redirect feature
Browse files Browse the repository at this point in the history
  • Loading branch information
cdhigh committed Jul 10, 2024
1 parent 2e28cfc commit bfe7271
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
18 changes: 15 additions & 3 deletions application/static/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ function showDictDialog(word, text, dictname, others) {
if (textDiv.attachShadow) { //使用shadow dom技术可以隔离css
if (!textDiv.shadowRoot) { //在第一个执行attachShadow后,这个变量会自动被设置
textDiv.attachShadow({mode: 'open'});
textDiv.shadowRoot.addEventListener('click', closeDictDialog);
//console.log('This browser supports Shadow DOM.');
}
textDiv.shadowRoot.innerHTML = text;
Expand Down Expand Up @@ -289,8 +290,10 @@ function closeDictDialog(event) {
var word = href.substring(24);
if (word) {
translateWord(word);
return;
return false;
}
} else if (href && g_allowLinks) {
openLinkInNewTab(href);
}
}

Expand Down Expand Up @@ -398,13 +401,22 @@ function iFrameEvent(event) {
document.getElementById('iframe').style.height = g_iframeScrollHeight + 'px';
} else if (data.type == 'click') {
if (data.href && g_allowLinks) {
window.location.href = data.href; //覆盖原先的阅读界面
openLinkInNewTab(data.href);
} else {
clickEvent(data.event);
}
}
}

//自适应在新tab中打开链接
function openLinkInNewTab(url) {
if (window.open) {
window.open(url, '_blank');
} else {
window.location.href = url;
}
}

//返回屏幕宽度
function getViewportWidth() {
return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
Expand Down Expand Up @@ -989,7 +1001,7 @@ function iframeLoadEvent(evt) {
event.preventDefault();
var href = target.getAttribute('href');
if (href && g_allowLinks) {
window.location.href = href; //kindle不支持window.open()
openLinkInNewTab(href);
return;
}
}
Expand Down
38 changes: 24 additions & 14 deletions application/templates/word_lookup.html
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,19 @@
//console.log('This browser supports Shadow DOM.');
if (!resultElement.shadowRoot) { //在第一个执行attachShadow后,这个变量会自动被设置
resultElement.attachShadow({mode: 'open'});
resultElement.shadowRoot.addEventListener('click', AClickEvent);
}
$(resultElement.shadowRoot).html(text);
resultElement.shadowRoot.innerHTML = text;
} else { //不支持shadow dom的浏览器
resultDiv.html(text);
}
}

//将一个新词添加到历史列表
function addToHistory(word) {
g_history.push(word);
if ((g_history.length == 0) || (g_history[g_history.length - 1] != word)) {
g_history.push(word);
}
g_currHisIdx = g_history.length - 1;
if (g_history.length > 1) {
$('#history-back').removeClass('pure-button-disabled');
Expand Down Expand Up @@ -214,6 +217,24 @@
}
}

//添加链接事件
function AClickEvent(event) {
var target = event ? event.target : null;
if (!target) {
return;
}
var href = $(target).attr('href') || '';
if (href.startsWith('https://kindleear/entry/')) {
event.preventDefault();
event.stopPropagation();
var word = href.substring(24);
if (word) {
startQueryDict(word);
addToHistory(word);
}
}
}

//注册各种事件
function regEvents() {
$("#search_for").keydown(function(event) {
Expand All @@ -224,18 +245,7 @@
});

//点击词典内的跳转,可以直接查其他词
$('#tr_result').on('click', 'a', function(event) {
var href = $(this).attr('href');
if (href.startsWith('https://kindleear/entry/')) {
event.preventDefault();
event.stopPropagation();
var word = href.substring(24);
if (word) {
startQueryDict(word);
addToHistory(word);
}
}
});
$('#tr_result').on('click', 'a', AClickEvent);
}

//自动选择上次使用的词典
Expand Down

0 comments on commit bfe7271

Please sign in to comment.