以下のように main.html で JavaScript main.js を呼び出しています。コードのように main.js 内で XMLHttpRequest を使っているのですが、コンテンツを取得することができません。具体的には hutteNippon.weatherXMLData.status が 0 となり、console.log() が実行されていません ( デバッガでチェックしました )。取得することができるようにするにはどうすればよいのでしょうか。
ちなみに XUL で拡張を書いていた時はコンテンツを取得できています。
main.html
コード: 全て選択
<!DOCTYPE html>
<html>
<head>
<title>Hütte Nippon 2</title>
<meta charset="UTF-8">
<script src="main.js"></script>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body id="main_box">
[略]
</body>
</html>
コード: 全て選択
(function(){
var global=this; // this は window
global.hutteNippon={}; // Global Scope
global.hutteNippon.prefecture="", // 都道府県
global.hutteNippon.region="", // 一次細分区域
global.hutteNippon.weatherXMLData={}; // 週間天気予報 RSS
[略]
function getWeatherXMLFile(){
var uri=endPointRss+hutteNippon.hLocationCode[hutteNippon.region];
hutteNippon.weatherXMLData=new XMLHttpRequest();
hutteNippon.weatherXMLData.open('GET',uri);
hutteNippon.weatherXMLData.onreadystatechange=function(){
if (hutteNippon.weatherXMLData.readyState === 4 && hutteNippon.weatherXMLData.status === 200){
console.log(hutteNippon.weatherXMLData.responseText);
}
}
hutteNippon.weatherXMLData.send(null);
}
[略]
function getLocation() {
var gettingLocation=browser.storage.local.get("location");
var location;
gettingLocation.then(results => {
if(results["location"]!=undefined){
location=results["location"];
hutteNippon.prefecture=location["prefecture"];
hutteNippon.region=location["region"];
console.log(hutteNippon.prefecture);
console.log(hutteNippon.region);
dispLocationButtonLavel();
getWeatherXMLFile();
}else{
console.log('not found location');
selectLocation();
}
});
}
[略]
function init() {
browser.windows.onRemoved.addListener((windowId) => {
browser.windows.onRemoved.removeListener(function(){
console.log('remove window remove lisener');
});
console.log("Closed window: "+windowId);
if(windowId==idWinLocation) getLocation();
});
console.log('init Main Window');
document.getElementById('close_button').addEventListener('click',closeWin);
document.getElementById('locationButton').addEventListener('click',selectLocation);
getLocation();
}
window.addEventListener('DOMContentLoaded',init);
})();