Skip to content

Commit

Permalink
v0.4🍉
Browse files Browse the repository at this point in the history
允许设置是否显示右键菜单、将默认语言设置为英文。
Merge branch 'developer'
  • Loading branch information
jinliming2 committed Jul 1, 2017
2 parents 2b3ddbb + 422ed3e commit c8ba673
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 27 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ A Google Chrome extension used to modify the page default encoding for Google Ch
[Google Chrome Web Store](https://chrome.google.com/webstore/detail/oenllhgkiiljibhfagbfogdbchhdchml)

## History
### v0.4(2017/7/1)
1. 新增设置选项:允许设置是否显示右键菜单(GitHub Issue:[#1](https://github.com/jinliming2/Chrome-Charset/issues/1)
2. 将默认语言设置为英文en,中文浏览器、英文浏览器不受影响,其他语言浏览器默认显示英文。(Chrome商店反馈)

### v0.3.1(2017/5/7)
1. 【修复】file协议下修改html文档编码失败的Bug。

Expand Down
6 changes: 5 additions & 1 deletion _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
"description": "Reset default charset"
},
"tipCurrent": {
"message": "Current",
"message": "Current: ",
"description": "The encoding used for the current page"
},
"settingMenu": {
"message": "ContextMenu: ",
"description": "Show Menu or Not"
},
"default": {
"message": "Default"
},
Expand Down
4 changes: 4 additions & 0 deletions _locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"message": "当前:",
"description": "The encoding used for the current page"
},
"settingMenu": {
"message": "显示右键菜单:",
"description": "Show Menu or Not"
},
"default": {
"message": "默认"
},
Expand Down
64 changes: 64 additions & 0 deletions c/popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,67 @@ button:hover {
button:active {
opacity: 1;
}

.setting {
display: flex;
justify-content: space-between;
align-items: center;
}

input[type=checkbox] {
visibility: hidden;
}

.switch {
flex-shrink: 0;
display: inline-block;
width: 120px;
height: 30px;
background: #f57c00;
-webkit-border-radius: 15px;
border-radius: 15px;
position: relative;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}

.switch:before {
content: 'On';
position: absolute;
top: 4px;
left: 13px;
height: 2px;
color: #fff;
font-size: 16px;
}

.switch:after {
content: 'Off';
position: absolute;
top: 4px;
left: 84px;
height: 2px;
color: #888;
font-size: 16px;
}

.switch label {
display: block;
width: 52px;
height: 18px;
cursor: pointer;
position: absolute;
top: 6px;
z-index: 1;
left: 12px;
background: #888;
-webkit-border-radius: 11px;
border-radius: 11px;
-webkit-transition: all 0.5s ease;
transition: all 0.5s ease;
}

.switch input[type=checkbox]:checked + label {
left: 60px;
background: #fff;
}
3 changes: 3 additions & 0 deletions j/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
* Created by Liming on 2017/2/14.
*/
"use strict";
const config = localStorage.getItem('config_menu');
localStorage.clear();
localStorage.setItem('config_menu', config);

const charsetPattern = /; ?charset=([^;]+)/;
const html_special_chars = (html) => html.replace(/&/g, '>')
.replace(/</g, '&lt;')
Expand Down
73 changes: 51 additions & 22 deletions j/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,39 @@
*/
"use strict";
(() => {
let _menu = false;
const menuList = {};
//CreateMenu
let menuList = {};
menuList['default'] = chrome.contextMenus.create({
type: "radio",
title: chrome.i18n.getMessage('default'),
checked: true,
onclick: (info, tab) => {
if(!info.wasChecked) {
resetEncoding(tab.id);
}
}
});
for(let encoding of ENCODINGS) {
if(encoding.length === 1) {
continue;
}
menuList[encoding[0].toUpperCase()] = chrome.contextMenus.create({
const createMenu = () => {
menuList['default'] = chrome.contextMenus.create({
type: "radio",
title: `${encoding[1]}${encoding[0]})`,
checked: false,
title: chrome.i18n.getMessage('default'),
checked: true,
onclick: (info, tab) => {
if(!info.wasChecked) {
setEncoding(tab.id, encoding[0]);
resetEncoding(tab.id);
}
}
});
}

for(let encoding of ENCODINGS) {
if(encoding.length === 1) {
continue;
}
menuList[encoding[0].toUpperCase()] = chrome.contextMenus.create({
type: "radio",
title: `${encoding[1]}${encoding[0]})`,
checked: false,
onclick: (info, tab) => {
if(!info.wasChecked) {
setEncoding(tab.id, encoding[0]);
}
}
});
}
_menu = true;
};
//UpdateMenu
let updateMenu = (tabId) => {
const updateMenu = tabId => {
let charset = getEncoding(tabId);
charset = charset ? menuList[charset.toUpperCase()] : menuList['default'];
for(let menu in menuList) {
Expand All @@ -45,17 +48,43 @@
});
};
chrome.tabs.onActivated.addListener((activeInfo) => {
if (!_menu) {
return;
}
updateMenu(activeInfo.tabId);
});
chrome.tabs.onUpdated.addListener((tabId) => {
if (!_menu) {
return;
}
updateMenu(tabId);
});
chrome.windows.onFocusChanged.addListener(() => {
if (!_menu) {
return;
}
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
if(tabs.length === 0) {
return;
}
updateMenu(tabs[0].id);
});
});

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch(message.action) {
case 'ShowMenu':
createMenu();
break;
case 'HideMenu':
_menu = false;
chrome.contextMenus.removeAll();
break;
}
});
const config = localStorage.getItem('config_menu');
if(config === 'false') {
return;
}
createMenu();
})();
10 changes: 10 additions & 0 deletions j/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
//I18N
document.getElementById('reset').innerHTML = chrome.i18n.getMessage('btnReset');
document.getElementById('tip_current').innerHTML = chrome.i18n.getMessage('tipCurrent');
document.getElementById('context_menu').innerHTML = chrome.i18n.getMessage('settingMenu');
//Reset
document.getElementById('reset').addEventListener('click', () => {
resetEncoding(tabs[0].id, () => {
Expand All @@ -58,5 +59,14 @@
});
list.appendChild(button);
}
//Settings
const setting_Menu = document.getElementById('menu');
setting_Menu.checked = localStorage.getItem('config_menu') !== 'false';
setting_Menu.addEventListener('change', _ => {
localStorage.setItem('config_menu', setting_Menu.checked);
chrome.runtime.sendMessage({
action: setting_Menu.checked ? 'ShowMenu' : 'HideMenu'
});
});
});
})();
6 changes: 3 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"manifest_version": 2,
"name": "__MSG_appName__",
"version": "0.3.1",
"default_locale": "zh_CN",
"version": "0.4",
"default_locale": "en",
"description": "__MSG_appDescription__",
"icons": {
"128": "i/128.png",
Expand Down Expand Up @@ -30,7 +30,7 @@
},
"content_scripts": [
{
"matches": ["*://*/*", "file://*/*"],
"matches": ["<all_urls>"],
"js": ["j/content.js"],
"run_at": "document_start"
}
Expand Down
10 changes: 9 additions & 1 deletion popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@
</head>
<body>
<div>
<span id="tip_current"></span><span id="current"></span>
<span id="tip_current"></span><span id="current"></span>
</div>
<button type="button" id="reset"></button>
<hr>
<div id="list"></div>
<hr>
<div class="setting">
<label id="context_menu"></label>
<div class="switch">
<input id="menu" type="checkbox">
<label for="menu"></label>
</div>
</div>
<script src="./j/encoding.js"></script>
<script src="./j/popup.js"></script>
</body>
Expand Down

1 comment on commit c8ba673

@jinliming2
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1

Please sign in to comment.