我不知道是谁投票结束了我的问题,但我很高兴它没有通过。其他人在网上其他地方也有这个问题。我还解释了我想要实现的目标,但是没有。
我最终使用了ipc-message。 documentation 可以为外行使用更多示例/解释,但是,嘿,我想通了。我的代码是here 和here,但如果我的代码由于某种原因消失,我也会在下面发布示例。
这段代码在aries.js,这个文件包含在主渲染器页面中,即index.html。
var ipc = require("ipc");
var webview = document.getElementsByClassName("tabs-pane active")[0];
webview.addEventListener("ipc-message", function (e) {
if (e.channel === "window-data") {
// console.log(e.args[0]);
$(".tab.active .tab-favicon").attr("src", e.args[0].favicon);
$(".tab.active .tab-title").html(e.args[0].title);
$("#url-bar").val(e.args[0].url);
$("#aries-titlebar h1").html("Aries | " + e.args[0].title);
}
// TODO
// Make this better...cancel out setTimeout?
var timer;
if (e.channel === "mouseover-href") {
// console.log(e.args[0]);
$(".linker").html(e.args[0]).stop().addClass("active");
clearTimeout(timer);
timer = setTimeout(function () {
$(".linker").stop().removeClass("active");
}, 1500);
}
});
下一段代码在browser.js,这个文件被注入到我的<webview>。
var ipc = require("ipc");
document.addEventListener("mouseover", function (e) {
var hoveredEl = e.target;
if (hoveredEl.tagName !== "A") {
return;
}
ipc.sendToHost("mouseover-href", hoveredEl.href);
});
document.addEventListener("DOMContentLoaded", function () {
var data = {
"title": document.title,
"url": window.location.href,
// need to make my own version, can't rely on Google forever
// maybe have this URL fetcher hosted on hikar.io?
"favicon": "https://www.google.com/s2/favicons?domain=" + window.location.href
};
ipc.sendToHost("window-data", data);
});
我还没有找到将 jQuery 注入<webview>s 的可靠方法,我可能不应该因为我要注入的页面可能已经有了它(如果你想知道为什么我的主要代码是 jQuery ,但也有常规的 JavaScript)。