本文介绍了在内容和后台之间传递的Chrome扩展消息无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 background.js getPageDimension = function(){ chrome.tabs.getSelected(null,function(tab) { chrome.tabs.sendMessage(tab.id,{message:DIMENSION},function(response){ if(response!= null){ console.log(response。 x); console.log(response.y); console.log(response.w); console.log(response.h); } else { console.log('Response is null'); } }); }); }; content.js $ b $ pre $ chrome.runtime.onMessage.addListener(函数(msg,sender,sendResponse){ if(msg .message&&(msg.message ==DIMENSION)){ var dimension = getPageDiemension(document.documentElement); console.log(dimension.x); console .log(dimension.y); console.log(dimension.w); console.log(dimension.h); sendResponse({x:dimension.x,y:dimension .y,w:dimension.w,h:dimension.h}); } }); getPageDiemension = function(currentDom){ var dimension = new Object(); dimension.x = 0; dimension.y = 0; dimension.w = currentDom.scrollWidth; dimension.h = currentDom.scrollHeight; 返回维度; $ / code> 所以我的目标是获得当前页面的全部高度和宽度活动标签。当我调试我的内容脚本时,我在 background.js 中获得了正确的响应,但是如果在未进行调试的情况下运行脚本,则会得到 undefined 在我的 background.js 中的回应。 这是我的文件中:c $ c> cotent.js all_frames:true,matches:[http:// * / *,https: // * / *,js:[content.js] }], 请帮助我,我哪里错了? 解决方案 问题 在你的代码中有两个小问题,我在发现一个错误之后发现它,因为它看起来很完美(当它完全不工作时)。 您正在使用不应使用的已弃用的 chrome.tabs.getSelected(...)方法。使用 chrome.tabs.query({active:true,highlight:true},...)。 在您的 chrome.runtime.onMessage 侦听器中,在内容脚本中,您在发送响应之前进行一些计算:这会使消息通道关闭,然后才能发送响应,并会导致 null 响应。 从官方文档: $ b 解决方案 现在您知道问题所在,您可以用 tabs.query()替换旧的 tabs.getSelected(),然后添加一个 return true; 语句在您的内容脚本中的 runtime.onMessage 事件的处理程序中。解决方案如下,我也稍微减轻了一些代码。 您的 background.js : getPageDimension = function(){ chrome.tabs.query({active:true,highlight:true},function(tabs ){ chrome.tabs.sendMessage(tabs [0] .id,{message:DIMENSION},function(response){ if(response!== null)console.log('Response :',response); else console.log('Response is null'); }); }); }; 您的 content.js : chrome.runtime.onMessage.addListener(函数(msg,sender,sendResponse){ if(msg.message&& amp; ;(msg.message ==DIMENSION)){ var dimension = getPageDiemension(document.documentElement); console.log('Dimension:',dimension); sendResponse ); } 返回true; }); $ b getPageDiemension = function(currentDom){ return {x:0,y:0,w:currentDom.scrollWidth,h:currentDom.scrollHeight $ b $ h $>工作示例 $ b 你可以找到一个我为测试做的实例 HERE 。 文档链接 为了清楚起见,我将留下一些文档 chrome.tabs API chrome.tabs.query 方法 Chrome扩展消息传递 chrome.tabs.sendMessage 方法 chrome.runtime.onMessage 事件 I am developing a chrome extension, here are the main files:background.jsgetPageDimension = function (){ chrome.tabs.getSelected(null, function(tab) { chrome.tabs.sendMessage(tab.id, { message: "DIMENSION" }, function(response){ if (response != null) { console.log(response.x); console.log(response.y); console.log(response.w); console.log(response.h); }else{ console.log('Response is null'); } }); }); };content.jschrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) { if (msg.message && (msg.message == "DIMENSION")) { var dimension = getPageDiemension(document.documentElement); console.log(dimension.x); console.log(dimension.y); console.log(dimension.w); console.log(dimension.h); sendResponse({x: dimension.x, y: dimension.y,w: dimension.w,h: dimension.h}); }});getPageDiemension = function(currentDom){ var dimension = new Object(); dimension.x = 0; dimension.y = 0; dimension.w = currentDom.scrollWidth; dimension.h = currentDom.scrollHeight; return dimension;}So my aim is to get the full height and width of page loaded in current active tab. When I debug my content script, I get the proper response in my background.js, but if run the script without debugging, I get an undefined response in my background.js.Here is the declaration of my cotent.js in my manifest.json file:"content_scripts": [{ "all_frames": true, "matches": [ "http://*/*", "https://*/*" ], "js": ["content.js"]}],Kindly help me, where am I going wrong?. Let me know if you need any further data. 解决方案 IssuesThere are two little problems in your code, which I found after a wile, since that it looks perfectly working (when it isn't at all).You're using the deprecated chrome.tabs.getSelected(...) method, which you should avoid. Use chrome.tabs.query({active: true, highlighted: true}, ...) instead.In your chrome.runtime.onMessage listener, in the content script, you are doing some calculations before sending the response: this makes the message channel close before you can send a response, and will result in a null response.Quoting from the official documentation:SolutionNow that you know what the problems are, you can easily replace the old tabs.getSelected() with tabs.query(), and add a return true; statement in the handler of the runtime.onMessage event in your content script. The solution is the following, I also lightened the code a bit.Your background.js:getPageDimension = function (){ chrome.tabs.query({active: true, highlighted: true}, function(tabs) { chrome.tabs.sendMessage(tabs[0].id, { message: "DIMENSION" }, function(response){ if (response !== null) console.log('Response:', response); else console.log('Response is null'); }); }); };Your content.js:chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) { if (msg.message && (msg.message == "DIMENSION")) { var dimension = getPageDiemension(document.documentElement); console.log('Dimension:', dimension); sendResponse(dimension); } return true;});getPageDiemension = function(currentDom){ return { x: 0, y: 0, w: currentDom.scrollWidth, h: currentDom.scrollHeight }}Working exampleYou can find a working example I made for testing HERE.Documentation linksJust for clarity, I'm leaving you some documentation links regarding the APIs involved in this extension that you may find helpful:chrome.tabs APIchrome.tabs.query methodChrome extension message passingchrome.tabs.sendMessage methodchrome.runtime.onMessage event 这篇关于在内容和后台之间传递的Chrome扩展消息无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-19 01:42