Skip to content

Instantly share code, notes, and snippets.

@Drunkar
Created December 26, 2020 09:45
Show Gist options
  • Save Drunkar/dc92eca84fbe87d2383f29005e469e92 to your computer and use it in GitHub Desktop.
Save Drunkar/dc92eca84fbe87d2383f29005e469e92 to your computer and use it in GitHub Desktop.
javascript: (function () {
/* Kindle Translator V 0.4*/
/* based on ACRExtensions via https://github.com/binarycrafts/ACRExtensions */
var w = null;
var kDoc = null;
var kObj = null;
if (typeof window.KindleReaderContextMenu !== 'undefined') {
w = window;
} else if (window.length) {
for (var i=0;i<window.length;i++) {
if (typeof window[i].KindleReaderContextMenu !== 'undefined') {
w = window[i];
break;
}
}
}
if (typeof w === 'object') {
kObj = w.KindleReaderContextMenu;
kDoc = w.document;
if (typeof kObj.ACRExtensions === 'undefined') {
kObj.ACRExtensions = true;
var oldMethod = kObj.show;
kObj.show = function () {
var res = oldMethod.apply(kObj, arguments);
var txtDoc = null;
var r = null;
if (typeof (arguments[3]) !== 'undefined' && typeof (arguments[3]['start']) !== 'undefined') {
var sId = arguments[3]['start'];
var eId = arguments[3]['end'];
$('iframe', kDoc).each(function (j, textIframe) {
var textIFrameDoc = $(textIframe).contents().get(0);
if ($('#'+sId, textIFrameDoc).get(0)) {
txtDoc = textIFrameDoc;
return false;
}
});
if (txtDoc) {
r = txtDoc.createRange();
r.setStartBefore($('#'+sId, txtDoc).get(0));
r.setEndAfter($('#'+eId, txtDoc).get(0));
}
}
$('#ACRExtensions_copyC', kDoc).remove();
var styles = $('<style>.spinner, .dictionary.i18n.expanded {display:none !important;} div#kindleReader_menu_contextMenu { max-height: 35px;}</style>');
var sepEl = $('<div id="ACRExtensions_copyC_sep" class="kindle_menu_separator"></div>');
var copyC = $('<div id="ACRExtensions_copyC" class="kindle_menu_button button_enabled ui-corner-left">Translate</div>');
$('#kindle_menu_border', kDoc).append(sepEl).append(copyC).append(styles);
setTimeout(function(){
sepEl.show();
copyC.removeClass('button_hidden');
}, 1);
$('#ACRExtensions_copyC', kDoc).click(function (evt) {
if (r) {
var newW = window.open('https://translate.google.com/?hl=ja&ie=utf-8#view=home&op=translate&sl=auto&tl=ja&text=' + encodeURIComponent(r), 'Google Translate' , "height=600,width=776,location=0,menubar=0,scrollbars=1,toolbar=0");
}
});
return res;
};
alert('Kindle Translator Extension is now active.');
} else {
alert('Kindle Translator Extension is already active.');
}
} else {
alert('Error: Kindle Translator Extension is not active. The Amazon Cloud Reader window could not be found.');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment