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

Show the popup only in Chinese pages #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions src/html/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ <h3>General</h3>
the page if you zoom in">[?]</span>
</td>
</tr>
<tr>
<td>
<input class="config" type="checkbox" id="showOnlyInChinesePage" name="scaleOnZoom">Show popup only in Chinese pages</input>
<span tooltip="If enabled, the popup will only show if the browser detects
Chinese in the current page, excluding Japanese or Korean.">[?]</span>
</td>
</tr>
</table>
</div>

Expand Down
7 changes: 6 additions & 1 deletion src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ chrome.runtime.onMessage.addListener(
switch(request.type) {
case 'enable?':
//chrome.tabs.sendMessage(sender.tab.id, {"type":"config", "config": liuChan.config.content});
if (request.enabled === false && liuChan.enabled) liuChan.onTabSelect(sender.tab);
// NOTE:
// `liuChan.onTabSelect()` handles the logic
// to check if the content script should be enabled, based on the detected language of the current page.
// Therefore `liuChan.onTabSelect()` should be invoked regardless of `liuChan.enabled`.
// if (request.enabled === false && liuChan.enabled) liuChan.onTabSelect(sender.tab);
if (request.enabled === false) liuChan.onTabSelect(sender.tab);
break;
case 'xsearch':
let e = liuChan.dict.wordSearch(liuChan.dict.hanzi, request.text);
Expand Down
17 changes: 11 additions & 6 deletions src/js/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ class Dictionary {
}

onDictionaryLoaded(tab) {
// Activate tab and send along content script related settings
if (this.lc.config.displayHelp === true && !this.lc.config.content.disableKeys) {
chrome.tabs.sendMessage(tab[0].id, {"type":"enable", "config": this.lc.config.content, "displayHelp": this.lc.displayHelp});
} else {
chrome.tabs.sendMessage(tab[0].id, {"type": "enable", "config": this.lc.config.content});
}
// Enable content script only if the detected language is Chinese.
chrome.tabs.detectLanguage(tab.id, (language) => {
if (!this.lc.config.content.showOnlyInChinesePage || language === "zh-CN" || language === "zh-TW") {
// Activate tab and send along content script related settings
if (this.lc.config.displayHelp === true && !this.lc.config.content.disableKeys) {
chrome.tabs.sendMessage(tab[0].id, {"type":"enable", "config": this.lc.config.content, "displayHelp": this.lc.displayHelp});
} else {
chrome.tabs.sendMessage(tab[0].id, {"type": "enable", "config": this.lc.config.content});
}
}
})
}

async checkForUpdates() {
Expand Down
17 changes: 11 additions & 6 deletions src/js/liuchan.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class LiuChan {
highlightText: true,
highlightInput: false,
scaleOnZoom: true,
showOnlyInChinesePage: false,
showOnKey: 0,
disableKeys: false
},
Expand Down Expand Up @@ -263,12 +264,16 @@ class LiuChan {
// The callback for chrome.tabs.onActivated
// Sends a message to the tab to enable itself if it hasn't
onTabSelect(tab) {
if (this.enabled) {
chrome.tabs.sendMessage(tab.tabId ? tab.tabId : tab.id, {
"type":"enable",
"config":this.config.content
});
}
// Enable content script only if the detected language is Chinese.
chrome.tabs.detectLanguage(tab.id, (language) => {
if (this.enabled &&
(!this.config.content.showOnlyInChinesePage || language === "zh-CN" || language === "zh-TW")) {
chrome.tabs.sendMessage(tab.tabId ? tab.tabId : tab.id, {
"type":"enable",
"config":this.config.content
});
}
});
chrome.tabs.sendMessage(tab.tabId ? tab.tabId : tab.id, {
"type":"update",
"notepad":this.config.notepad
Expand Down
3 changes: 3 additions & 0 deletions src/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function init() {
checkboxUsePinyinToneColors: d.getElementById('usePinyinToneColors'),
checkboxDisplayHelp: d.getElementById('displayHelp'),
checkboxScaleOnZoom: d.getElementById('scaleOnZoom'),
checkboxShowOnlyInChinesePage: d.getElementById('showOnlyInChinesePage'),
selectLineEnding: d.getElementById('lineEnding'),
selectCopySeparator: d.getElementById('copySeparator'),
inputMaxCopyEntries: d.getElementById('maxClipCopyEntries'),
Expand Down Expand Up @@ -104,6 +105,7 @@ function saveOptions() {
highlightText: e.checkboxHighlightText.checked,
highlightInput: e.checkboxHighlightInput.checked,
scaleOnZoom: e.checkboxScaleOnZoom.checked,
showOnlyInChinesePage: e.checkboxShowOnlyInChinesePage.checked,
showOnKey: parseInt(document.querySelector('input[name="showOnKey"]:checked').value),
disableKeys: e.checkboxDisableKeys.checked
},
Expand Down Expand Up @@ -166,6 +168,7 @@ function restoreOptions() {
e.checkboxHighlightText.checked = items.content.highlightText;
e.checkboxHighlightInput.checked = items.content.highlightInput;
e.checkboxScaleOnZoom.checked = items.content.scaleOnZoom;
e.checkboxShowOnlyInChinesePage.checked = items.content.showOnlyInChinesePage;
// showOnKey radio button - see below
e.checkboxDisableKeys.checked = items.content.disableKeys;

Expand Down