所以我知道这与AJAX有关,但我已经阅读了一些主题并且不太了解我必须做什么。目前一切正常,但我必须刷新页面才能运行我的脚本。我的代码需要做些什么才能让它无需刷新?
// ==UserScript==
// @name Job Aids
// @description Aid in closing tickets
// @include https://techaccess.ad.qintra.com/WorkJobs/WorkJobs.aspx*
// @namespace camzilla.net
// @version 1.1.20121128
// ==/UserScript==
var url = window.location.href.split(".aspx");
var page = url[1].toLowerCase();
if (page == "#finaltest") {
window.addEventListener ("load", finalResults, false);
} else if (page == "#threetoneslope") {
window.addEventListener ("load", toneSlopeResults, false);
} else if (page == "#codes") {
window.addEventListener ("load", closingComments, false);
} else if (page == "#cras") {
window.addEventListener ("load", crasResults, false);
} else if (page == "#jobinfo") {
window.addEventListener ("load", addLinks, false);
} else if (page == "") {
if (getCookie("updater") == null) {
var d = new Date();
setCookie("updater", d.getTime(), 1);
try {
updateCheck();
} catch(err) {
// alert('Update checking failed');
}
}
// setTimeout(promptDispatch, 1000);
}
function addLinks() {
var mydiv = document.querySelector('div[data-bind="CurrentJob.addr"]');
var address = 'https://maps.google.com/maps?q=' + mydiv.textContent + ", UT";
address.replace(/ /g, '+');
var a = document.createElement("a");
var txt = document.createTextNode(mydiv.textContent);
mydiv.textContent = "";
a.appendChild(txt);
a.href = address;
a.target = '_blank';
mydiv.appendChild(a);
mydiv = document.querySelector('div[data-bind="CurrentJob.cktid"]');
address = 'http://acmspjv1.interprise.com/cgi-bin/QC/DSL/dslam6100Int.pl?telephoneNum=';
address+= mydiv.textContent;
a = document.createElement("a");
txt = document.createTextNode(mydiv.textContent);
mydiv.textContent = "";
a.appendChild(txt);
a.href = address;
a.target = '_blank';
mydiv.appendChild(a);
}
function updateCheck() {
try {
GM_xmlhttpRequest(
{
method: 'GET',
url: 'http://camzilla.net/downloads/jobAids.user.js?v' + getCookie("updater"),
headers: {'Cache-Control': 'no-cache'},
onload: function(resp) {
var local_version, remote_version, rt, script_name;
rt = resp.responseText;
remote_version = (/@version\s*(.*?)\s*$/m.exec(rt)[1]);
local_version = GM_info.script.version;
if (remote_version != local_version) {
if(confirm(GM_info.script.name+' update v'+remote_version+ ' is available.\nWould you like to install it now?')) {
GM_openInTab('http://camzilla.net/downloads/jobAids.user.js');
} else {
alert('You will be reminded again tomorrow');
}
}
}
});
} catch(err) {
// do something here
}
}
function setCookie(c_name, value, exdays) {
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name) {
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++) {
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name) {
return unescape(y);
}
}
return null;
}
function finalResults() {
var dc, current, loss, noise, pi, lb;
dc = getRandom(51, 49, 2);
current = getRandom(37, 29, 2);
loss = getRandom(7, 2, 2);
noise = getRandom(7, 1, 2);
pi = getRandom(67, 60, 2);
lb = getRandom(79, 70, 2);
document.getElementById('finalTestInsightNo').click();
document.getElementById("finalTestBeforeAcTg").value = "0.00";
document.getElementById("finalTestBeforeAcRg").value = "0.00";
document.getElementById("finalTestBeforeAcTr").value = "0.00";
document.getElementById("finalTestBeforeDcTr").value = dc;
document.getElementById("finalTestBeforeDcTg").value = "0.00";
document.getElementById("finalTestBeforeDcRg").value = dc;
document.getElementById("finalTestBeforeLoopCurrent").value = current;
document.getElementById("finalTestAfterLoss").value = loss;
document.getElementById("finalTestAfterNoise").value = noise;
document.getElementById("finalTestAfterPi").value = pi;
document.getElementById("finalTestAfterLb").value = lb;
document.getElementById("finalTestAfterLb").focus();
window.scrollTo(0, document.body.scrollHeight-250);
// Store cookies to be used on cras page if needed
setCookie("loss", loss, 1);
setCookie("pi", pi, 1);
setCookie("current", current, 1);
setCookie("noise", noise, 1);
setCookie("lb", lb, 1);
}
function crasResults() {
document.getElementById("crasCircuitLoss").value = getCookie("loss");
document.getElementById("crasPowerInfluence").value = getCookie("pi");
document.getElementById("crasLineCurrent").value = getCookie("current");
document.getElementById("crasCircuitNoise").value = getCookie("noise");
document.getElementById("crasBalance").value = getCookie("lb");
document.getElementById('crasFoundInF2').click();
}
function toneSlopeResults() {
var dc = getRandom(51, 49, 2);
document.getElementById("ToneSlopeInsightNo").click();
document.getElementById("ToneSlopeDmarcTaggedYes").click();
document.getElementById("ToneSlopeClecDtPresentYes").click();
document.getElementById("ToneSlopeBeforeDcTr").value = dc;
document.getElementById("ToneSlopeBeforeDcTg").value = "0.00";
document.getElementById("ToneSlopeBeforeDcRg").value = dc;
document.getElementById("ToneSlopeRingbackTr").value = getRandom(90,85,2);
document.getElementById("ToneSlopeAfterResistTg").value = getRandom(998,20,0);
document.getElementById("ToneSlopeAfterResistRg").value = getRandom(998,20,0);
document.getElementById("ToneSlopeAfterResistTr").value = getRandom(998,20,0);
document.getElementById("ToneSlopeAfter404").value = "0.00";
document.getElementById("ToneSlopeAfter1004").value = getRandom(7, 3, 2);
document.getElementById("ToneSlopeAfter2804").value = "0.00";
document.getElementById("ToneSlopeAfterNoise").value = getRandom(7, 1, 2);
document.getElementById("ToneSlopeAfterLb").value = getRandom(79, 70, 2);
document.getElementById("ToneSlopeCotName").value = "NA";
document.getElementById("ToneSlopeTn").focus();
}
function closingComments() {
document.getElementById("flatRateJacks").value = "0";
document.getElementById("rewiredJacks").value = "0";
document.getElementById("tripNo").click();
document.getElementById("marketMass").click();
document.getElementById("custTypeRes").click();
document.getElementById("pairChangeNo").click();
document.getElementById("tempDropNo").click();
document.getElementById("goodCbrYes").click();
document.getElementById("jobCloseComments").focus();
window.scrollTo(0, 5000);
}
function getRandom(max, min, tenths) {
var result = min + (Math.random() * (max - min + 1));
return result.toFixed(tenths);
}
答案 0 :(得分:6)
有关详情和类似情况,请参阅"addEventListener only working at page refresh?"。
在load
事件被触发后,毫无疑问会出现您的脚本所期望的页面元素。另外,从您的评论中,听起来好像AJAX交换了页面的整个部分,但AJAX足以礼貌地更改URL哈希。这意味着您需要启动hashchange
事件。
在这种情况下,请勿使用addEventListener ("load"...
。将the waitForKeyElements() utility与hashchange
结合使用。
如果不重构整个脚本以使用jQuery(这将提供更清晰,更健壮的代码),请在function addLinks() {...
之前替换所有内容,并使用:
// ==UserScript==
// @name Job Aids
// @description Aid in closing tickets
// @include https://techaccess.ad.qintra.com/WorkJobs/WorkJobs.aspx*
// @namespace camzilla.net
// @version 1.1.20121128
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
//-- Pages are "loaded" via AJAX...
window.addEventListener ("hashchange", fireOnNewPage, false);
waitForKeyElements ("#crasCircuitLoss", crasResults);
waitForKeyElements ("#finalTestInsightNo", finalResults);
waitForKeyElements ("#flatRateJacks", closingComments);
waitForKeyElements ("#ToneSlopeInsightNo", toneSlopeResults);
waitForKeyElements ("div[data-bind="CurrentJob.addr"]", addLinks);
function fireOnNewPage () {
switch (location.hash.toLowerCase() ) {
case "#finaltest":
case "#threetoneslope":
case "#codes":
case "#cras":
case "#jobinfo":
//-- No action needed, waitForKeyElements() handles this.
break;
default:
if (getCookie("updater") == null) {
var d = new Date();
setCookie("updater", d.getTime(), 1);
try {
updateCheck();
} catch(err) {
// alert('Update checking failed');
}
}
break;
}
}
fireOnNewPage (); //-- Initial run on initial, full page load.
答案 1 :(得分:-1)
通过搜索查看网上的一些ajax示例。例如:http://www.w3schools.com/ajax/ajax_examples.asp
AJAX,或(A)同步(J)avascript(A)nd(X)ML(有趣的是,这些天有时候更倾向于使用JSON),是一个Javascript使用浏览器对象与远程通信的系统服务器。这种情况的一般用例是能够更新客户端的界面而无需转到另一个页面。在我们开始之前,请注意几点。
建议不要将Ajax用于登录身份验证和发布表单 用户可以关闭Javascript,或者由于IT策略而限制运行Javascript
考虑到这一点,建议您不要将AJAX用作关键用户功能的唯一解决方案!总是有一个后备! 注意:这个社区wiki帖子使用JQuery来显示示例AJAX调用。它建议新手使用,因为它隐藏了制作AJAX调用的浏览器兼容性问题。有关JQuery的更多信息,请查看JQuery网站。
注意:这些示例使用与PHP服务器的通信,但任何服务器端语言都可以使用。
AJAX回调
function makeAjaxCall() {
$.ajax({
url: 'ajax/test.html',
success: function(data) {
alert('Horray the AJAX call succeeded!');
},
error: function(xhr, error) {
alert('Holy errors batman!');
}
});
}
AJAX呼叫的本质
AJAX调用可以是异步调用也可以是同步调用。异步意味着浏览器将发出AJAX请求并继续执行其他操作。同步意味着浏览器将停止正在进行的操作,直到AJAX调用完成。以下是两个代码方面的差异示例:
// An asynchronous call
// This is the default
$.ajax({
url: '/server.php',
success: function(data) {
alert('Horray the AJAX call succeeded!');
},
error: function(xhr, error) {
alert('Holy errors batman!');
}
});
// This will get called right away
myFunction();
Now for a synchronous call:
// A synchronous call
$.ajax({
url: '/server.php',
async: false, // set the property here
success: function(data) {
alert('Horray the AJAX call succeeded!');
},
error: function(xhr, error) {
alert('Holy errors batman!');
}
});