等待 chrome.storage.local.get() 完成

chrome.storage.local.get() 获取存储处理并计数,内部计数正常,外部使用始终为0,百思不得其解。

如何在继续执行之前等待异步chrome.storage.local.get()完成-腾讯云开发者社区-腾讯云 (tencent.com)

原来我忽略了异步问题,最简单的方案就是一次取多个值,并在内部完成所有操作!

javascript 复制代码
chrome.storage.local.get(['is_light', 'is_hide', 'keyword_light', 'keyword_hide'], function (result) {
    //console.log(result);
	
	if (result.is_light) {
	    var s = result.keyword_light;
	    var sl = s.split(';');
	    var a = document.getElementsByTagName('a');
	    for (var i = 0; i < a.length; i++) {
	        for (var j = 0; j < sl.length; j++) {
	            if (a[i].textContent.indexOf(sl[j]) != -1) {
	                a[i].style.color = 'white';
	                a[i].style.backgroundColor = '#DA3434';
	                a[i].innerHTML = a[i].textContent;
	                count_light++;
	            }
	        }
	    }
	}
	
	if (result.is_hide) {
		var s = result.keyword_hide;
		var sl = s.split(';');
		var a = document.getElementsByTagName('a');
		for (var i=0; i<a.length; i++) {
			for (var j=0; j<sl.length; j++) {
				if (a[i].innerHTML.indexOf(sl[j]) != -1) {
					a[i].style.display = 'none';
					count_hide++;
				}
			}
		}
	}
	
	var s = count_light + '+' + count_hide;
	chrome.runtime.sendMessage({ badgeText: s }, function(response) {
		//console.log('return: ' + response.farewell);
	});

});
相关推荐
GISer_Jing2 小时前
Three.js着色器编译机制深度解析
javascript·webgl·着色器
丷丩2 小时前
MapLibre GL JS第22课:查看本地GeoJSON
前端·javascript·map·mapbox·maplibre gl js
油炸自行车3 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
丷丩7 小时前
MapLibre GL JS第19课:实时更新要素
前端·javascript·gis·map·mapbox·maplibre gl js
xiaohua0708day8 小时前
Lodash库
前端·javascript·vue.js
突然好热8 小时前
TS 调试技巧
前端·javascript·typescript
h64648564h8 小时前
Flutter 国际化(i18n)全指南:一键切换中/英/日多语言
前端·javascript·flutter
丷丩9 小时前
MapLibre GL JS第8课:禁用滚动缩放
javascript·mapbox·maplibre gl js
kyriewen11 小时前
面试8家前端岗位后,我发现了一个残酷的事实:AI不是加分项,是门槛
前端·javascript·面试
MageGojo14 小时前
做节日活动页时,如何用 API 快速生成对联内容
javascript·python·节日·对联生成