Chrome/Firefox のユーザー スクリプトを作成するのは初めてです。AJAXで生成されたJSONデータをWebサイトから取得して自分のサーバーに送信し、分析してデータベースに入れようとしていました。
Ajax を使用してデータを送信し、自分のサーバー上のテキスト ファイルに入れることができました。しかし、Chrome コンソールでは、次のように表示されます。
キャッチされていない SyntaxError: 予期しないトークン j
、しかし、 「データが送信されました」と「通話後」であるという各メッセージを既に取得しています-これらはログメッセージです。
実装する必要があるコールバックはありますか、それとも明らかなものがありませんか? ここにコードの短縮バージョンを掲載しています。
// Injecting javascript in the original page
function inject(func) {
var source = func.toString();
var script = document.createElement('script');
script.innerHTML = "(" + source + ")()";
document.body.appendChild(script);
}
function injection() {
}
// This function intercepts the ajax
function interceptAjax() {
$('body').ajaxSuccess (
function (event, requestData, settings) {
serverData = requestData.responseText;
if(JSON.parse(settings.data).method == "dashboard.getPaginatedPlextsV2"){
console.log("Sending");
$.ajax({
type: "POST",
url: "http://mywebsite.com/collectdata.php",
data: { json: JSON.stringify(serverData) },
success: function(msg) {
console.log("Data were sent");
},
error: function(xhr, ajaxOptions, thrownError) {
console.log("Failed: " + xhr.status + " - " + thrownError);
}
});
console.log("After the call");
}
}
);
}
// A helperfunction for the ajaxInterception to work in Chrome
function addJS_Node (text, s_URL, funcToRun) {
var D = document;
var scriptNode = D.createElement('script');
scriptNode.type = "text/javascript";
if(text) scriptNode.textContent = text;
if(s_URL) scriptNode.src = s_URL;
if(funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
var targ = D.getElementsByTagName('head')[0] || D.body || D.documentElement;
targ.appendChild(scriptNode);
}
// This function is necessary to intercept the ajax
addJS_Node(null, null, interceptAjax);
inject(injection);