我正在尝试返回在 google-chrome 扩展中的页面上找到的一些元数据。background.js 向 website.js 页面发出元数据请求。该请求是在页面更新内容时正确发出的,因此我必须在返回之前检查以确保数据已被修改。这是我的代码:
var checkMeta = function(){
var title = $(".playerBarSong").text();
var artist = $(".playerBarArtist").text();
var album = $(".playerBarAlbum").text();
var art = $(".playerBarArt").attr("src");
if (previousTitle != title || previousArtist != artist || previousAlbum != album || previousArt != art){
previousTitle = title;
previousArtist = artist;
previousAlbum = album;
previousArt = art;
currentMeta.title = title;
currentMeta.artist = artist;
currentMeta.album = album;
currentMeta.art = art;
window.clearInterval(intervalId);
sendResponse(currentMeta); //Does not return value to the extension
}
};
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse){
if (request.request == "meta"){
currentMeta = request.meta;
intervalId = window.setInterval(checkMeta,250)
//Some sort of callback?
}
}
);
香草 JavaScript 或 jquery 中的任何解决方案将不胜感激。