Skip to content

Instantly share code, notes, and snippets.

@mpco
Created January 13, 2019 13:11
Show Gist options
  • Save mpco/114365cf21964a433087fd5c1446745c to your computer and use it in GitHub Desktop.
Save mpco/114365cf21964a433087fd5c1446745c to your computer and use it in GitHub Desktop.
英语阅读-JSBox
// $objc("UIReferenceLibraryViewController").invoke("dictionaryHasDefinitionForTerm:", "book");
// let refVC = $objc("UIReferenceLibraryViewController").invoke("alloc").invoke("initWithTerm:", "book");
// let rootVC = $ui.controller.runtimeValue();
// rootVC.$presentViewController_animated_completion(refVC, true, null);
// $text.lookup("text");
if ($app.env == $env.action) {
var baseURI = $context.link
// $ui.alert(baseURI);
var scriptFullName = $addin.current.name
var scriptName = scriptFullName.substring(0, scriptFullName.length - 3);
var runApp = "jsbox://run?name=" + scriptName + "&baseURI=" + baseURI
$app.openURL(runApp);
} else if ($app.env == $env.safari) {
var items = $context.safari.items
var baseURI = items.baseURI
var scriptFullName = $addin.current.name
var scriptName = scriptFullName.substring(0, scriptFullName.length - 3);
var runApp = "jsbox://run?name=" + scriptName + "&baseURI=" + baseURI
$app.openURL(runApp);
} else if ($app.env == $env.app) {
var query = $context.query;
var webURL = query.baseURI;
if (webURL == undefined) {
$ui.alert("未开发")
$app.close();
}
$ui.render({
views: [
{
type: "web",
props: {
url: webURL,
script: function () {
var allP = document.getElementsByTagName('p');
for (var i = 0; i < allP.length; i++) {
var element = allP[i];
var words = element.innerText.split(" ");
element.innerText = "";
for (var j = 0; j < words.length; j++) {
var newSpan = document.createElement("span");
newSpan.innerText = words[j] + " ";
newSpan.onclick = function (e) {
var t = e.target || e.srcElement;
return $notify("share", { text: t.innerText }),!1;
};
element.appendChild(newSpan);
};
}
}
},
layout: $layout.fill,
events: {
share: function (object) {
var word = object.text.replace(/[?!:\s\.]+$/gmi, "")
$text.lookup(word);
}
}
}
]
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment