我一直在尝试为Google Chrome编写我的第一个用户脚本。它会检查路由器页面并重新连接,直到获得有效的IP地址。
我将脚本另存为IPchecker.user.js,并通过扩展页面将其安装在google chrome中。但是当我刷新页面时,javascript控制台抛出一个错误:
Uncaught ReferenceError: wan_connect is not defined 在本例中,wan_connect是在原始页面上定义的函数:
<html>
<head>
<script type="text/javascript">
function serv(service, sleep)
{
form.submitHidden('service.cgi', { _service: service, _redirect: 'status-overview.asp', _sleep: sleep });
}
function wan_connect(wanid)
{
if (wanid == "1") {
serv('wan1link-restart', 5);
}
if (wanid == "2") {
serv('wan2link-restart', 5);
}
}
//All other stuff deleted...
</script>
</head>
<body> Also deleted...</body>
</html>这是我的脚本。看起来它不能识别来自原始页面的函数。有没有办法叫它?
// ==UserScript==
// @name WAN checker
// @version 0.0.1
// @description Check tomato IP addresses and reconnect as needed
// @match *://192.168.1.1/status-overview.asp
// ==/UserScript==
(function reconnect() {
wan1ip = document.getElementById('wanip').getElementsByClassName('content')[0].innerText.split('.').slice(0,2).join('.');
if (wan1ip != "333.3333") {
wan_connect("1");
setTimeout("reconnect()",2000);
}
wan2ip = document.getElementById('wan2ip').getElementsByClassName('content')[0].innerText.split('.').slice(0,2).join('.');
if (wan2ip != "333.333") {
wan_connect("2");
setTimeout("reconnect()",2000);
}
})();发布于 2012-10-13 06:01:58
是的,您可以通过以下几种方式调用该代码:
或
或
GM_xmlhttpRequest()抓取原始页面的源代码。从源代码中提取适当的脚本节点,并将该代码注入到当前目标页中。在以前的userscripts和greasemonkey问题中,在某种程度上已经涵盖了所有这些方法。
发布于 2012-10-13 05:25:41
不能,您不能从一个页面调用另一个页面上的脚本。您必须将代码包含在需要使用它的每个页面中。
有一些用于脚本加载的库(例如RequireJS)可以用来将脚本从JavaScript加载到页面中,但是您仍然需要有可用的脚本才能以这种方式加载。
https://stackoverflow.com/questions/12867300
复制相似问题