以下のコードで、jsonをconsoleに表示させたいと思っています。
デベロッパーツールで実行すると、うまく動くのですが、
var obj; var json; // Load the script var script = document.createElement("SCRIPT"); script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'; script.type = 'text/javascript'; script.onload = function() { var $ = window.jQuery; obj = $.getJSON('https://yuis.xsrv.jp/cdn/dev.json', function(data) {}); }; document.getElementsByTagName("head")[0].appendChild(script); json = obj.responseJSON ; console.log(json.keywords) ;
tampermonkeyだと、objが定義されていない、というエラーが出てしまいます。
// ==UserScript== // @name dev2 // @namespace http://tampermonkey.net/ // @version 0.1 // @description try to take over the world! // @author You // @match *://*/* // @grant none // ==/UserScript== var obj; var json; // Load the script var script = document.createElement("SCRIPT"); script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'; script.type = 'text/javascript'; script.onload = function() { var $ = window.jQuery; // Use $ here... // obj = $.getJSON('http://localhost:3000/api/index.json?url[]=https://www.microsoft.com/ja-jp/windows&url[]=https://en.wikipedia.org/wiki/Microsoft_Windows', function(data) {}); obj = $.getJSON('https://yuis.xsrv.jp/cdn/dev.json', function(data) {}); }; document.getElementsByTagName("head")[0].appendChild(script); json = obj.responseJSON ; console.log(json.keywords) ;
エラー
ERROR: Execution of script 'dev2' failed! Cannot read property 'responseJSON' of undefined obj VM987:1 Uncaught ReferenceError: obj is not defined at <anonymous>:1:1
どこか抜けている部分などありますでしょうか?

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。