我的代码运行良好,但是当我重新加载网页时,该代码将不再起作用。我也尝试使用本地存储,但仍然无法正常工作。可能是这个问题的原因?
有我的popup.js:
window.addEventListener('DOMContentLoaded', function(){
let icons = document.querySelectorAll('.icon');
for( let icon of icons){
icon.addEventListener('click',function(){
let imgSrc = icon.src;
chrome.runtime.sendMessage({message: "changeCursor", img: imgSrc});
});
} ;
});
background.js:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.message = "changeCursor"){
chrome.tabs.query({}, function(tabs) {
for (let i=0; i<tabs.length; ++i) {
let imgSrc = request.img;
chrome.storage.local.set({key: imgSrc}, function() {
console.log('Value is set to ' + imgSrc);
});
chrome.tabs.sendMessage(tabs[i].id,{message: "startChangeCursor", } );
}
});
}
});
content.js:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if(request.message = "startChangeCursor"){
chrome.storage.local.get(['key'], function(result) {
$("body").css('cursor', `url(${result.key}) 1 1, auto`);
});
}
});