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

Plugin for the online dictionary multitran.ru #524

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
Binary file added PluginDirectories/1/multitran.bundle/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
164 changes: 164 additions & 0 deletions PluginDirectories/1/multitran.bundle/content.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<html>
<head>
<style>
#plugincontent {
width: 100%;
height: 100%;
display: table;
}
body {
overflow-x: hidden;
}
tr {
vertical-align: top;
}
table {
word-break: break-word;
max-width: 360px;
}

td {
max-width: 350px;
padding: 0 10px 0 0;
}

.errorImg {
margin: 0 50px;
}

.spinner {
margin: 100px auto 0;
width: 70px;
text-align: center;
}

.spinner > div {
width: 18px;
height: 18px;
background-color: #333;

border-radius: 100%;
display: inline-block;
-webkit-animation: bouncedelay 1.4s infinite ease-in-out;
animation: bouncedelay 1.4s infinite ease-in-out;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}

.spinner .bounce1 {
-webkit-animation-delay: -0.32s;
animation-delay: -0.32s;
}

.spinner .bounce2 {
-webkit-animation-delay: -0.16s;
animation-delay: -0.16s;
}

@-webkit-keyframes bouncedelay {
0%, 80%, 100% { -webkit-transform: scale(0.0) }
40% { -webkit-transform: scale(1.0) }
}

@keyframes bouncedelay {
0%, 80%, 100% {
transform: scale(0.0);
-webkit-transform: scale(0.0);
} 40% {
transform: scale(1.0);
-webkit-transform: scale(1.0);
}
}
</style>
</head>
<body>
<div id="plugincontent">
<div class="spinner">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>

<script>
var words = "#words#";
var multitranRoot = "http://www.multitran.ru";
var multitranBase = "http://www.multitran.ru/c/";

function fetchContent(words, completion, failure) {
var req = new XMLHttpRequest();
var url = multitranBase + "m.exe?CL=1&l1=1&s=" + words;
req.open("GET", url);
req.onreadystatechange = function() {
if (req.readyState != 4)
return;
if (req.status == 200) {
var doc = document.createElement("div");
doc.innerHTML = req.responseText;
completion(doc);
} else {
failure();
}
}
req.send(null);
}

function patchRefs(contentRoot, tagName, attributeName) {
var elems = contentRoot.getElementsByTagName(tagName);
for (var i = 0; i < elems.length; ++i) {
var e = elems[i];
var href = e.getAttribute(attributeName);
if (href && href.indexOf("://") === -1) {
if (href.indexOf("/") === 0)
e.setAttribute(attributeName, multitranRoot + href);
else
e.setAttribute(attributeName, multitranBase + href);
}
}
}

function processContent(content, completion, failure) {
var form = content.querySelector("#translation");
if (!form) {
failure();
return;
}

var cell = form.parentNode;
while (cell.childNodes.length > 0 && cell.childNodes[0].nodeName != "FORM") {
var cellHtml = cell.innerHTML;
cell.removeChild(cell.childNodes[0]);
}
while (cell.childNodes.length > 0 && cell.childNodes[0].nodeName != "TABLE") {
var cellHtml = cell.innerHTML;
cell.removeChild(cell.childNodes[0]);
}

patchRefs(cell, "IMG", "SRC");
patchRefs(cell, "A", "HREF");

return completion(cell);
}

function displayContent(contentRoot) {
var container = document.getElementById("plugincontent");
container.innerHTML = contentRoot.innerHTML;
}

var failure = function() {
var container = document.getElementById("plugincontent");
container.innerHTML = "<img src='error.png' class='errorImg'>";
}

setTimeout(function() {
fetchContent(words, function(doc) {
processContent(doc, function(contentRoot) {
displayContent(contentRoot);
},
failure);
},
failure);
}, 500);
</script>
</body>
</html>
Binary file added PluginDirectories/1/multitran.bundle/error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions PluginDirectories/1/multitran.bundle/examples.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mt ~words(words or phrases)
multitran ~words(words or phrases)
multitran.ru ~words(words or phrases)
мт ~words(words or phrases)
ье ~words(words or phrases)
мультитран ~words(words or phrases)
ьгдешекфт ~words(words or phrases)
9 changes: 9 additions & 0 deletions PluginDirectories/1/multitran.bundle/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "multitran",
"displayName": "Multitran.ru",
"description": "Just great dictionary in your Spotlight",
"examples": ["mt translator", "multitran tezaurus", "мт пример", "ьгдешекфт пример"],
"categories": ["Language"],
"creator_name": "Denis St",
"creator_url": "http://multitran.ru/"
}
21 changes: 21 additions & 0 deletions PluginDirectories/1/multitran.bundle/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import urllib

def results(fields, original_query):
words = fields['~words'].encode('UTF-8')
words_escaped = urllib.quote_plus(words)

url = "http://www.multitran.ru/c/m.exe?CL=1&l1=1&s=" + words_escaped
html = open("content.html").read().replace("#words#", words_escaped)
return {
"title": "Translation for %s" % (words),
"run_args": [url],
"html": html,
"webview_user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53",
"webview_links_open_in_browser": True,
"webview_transparent_background": True
}


def run(url):
import os
os.system('open "{0}"'.format(url))