chrome又出幺蛾子了之Cannot read property 'onMessage' of undefined?


声明:本文转载自https://my.oschina.net/qixiaobo025/blog/1817653,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

背景

Chrome获取Mac地址实现

问题

这个问题其实还蛮影响开发和测试的

小伙伴加的代码如下

var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1;//判断是否是chrome浏览器 //获取插件mac地址 if(isChrome){        var editorExtensionId = "fndijacmgfpgicgknoceclheehanolhc";        function sendMessage(type){            chrome.runtime.sendMessage(editorExtensionId, {requestType: type},                function(response) {                });        }        sendMessage(); }

其实很明显这段代码容易出问题!

首先chrome.runtime从chrome23版本才有 那么不兼容老版本的chrome。

chrome.runtime

Description: Use the chrome.runtime API to retrieve the background page, return details about the manifest, and listen for and respond to events in the app or extension lifecycle. You can also use this API to convert the relative path of URLs to fully-qualified URLs. 
Availability: Since Chrome 23. 
Content Scripts: connect , getManifest , getURL , id , onConnect , onMessage and sendMessage are supported. Learn more 
Learn More: Manage App Lifecycle 
Event Pages 

其次sendMessage出现的更晚

sendMessage

chrome.runtime.sendMessage(string extensionId, any message, object options, function responseCallback) 

Since Chrome 26.

Sends a single message to event listeners within your extension/app or a different extension/app. Similar to runtime.connect but only sends a single message, with an optional response. If sending to your extension, the runtime.onMessage event will be fired in every frame of your extension (except for the sender's frame), or runtime.onMessageExternal, if a different extension. Note that extensions cannot send messages to content scripts using this method. To send messages to content scripts, use tabs.sendMessage.

Parameters
string (optional)extensionId

The ID of the extension/app to send the message to. If omitted, the message will be sent to your own extension/app. Required if sending messages from a web page for web messaging.

any message

The message to send. This message should be a JSON-ifiable object.

object (optional) options

Since Chrome 32.

boolean (optional)includeTlsChannelId

Whether the TLS channel ID will be passed into onMessageExternal for processes that are listening for the connection event.

function (optional)responseCallback

If you specify the responseCallback parameter, it should be a function that looks like this:

function(any response) {...};

any response

The JSON response object sent by the handler of the message. If an error occurs while connecting to the extension, the callback will be called with no arguments and runtime.lastError will be set to the error message.

我们在一段时间之后【目前更新到chrome66】发现某些网页不能获取到chrome.runtime由此导致报错。

分析

首先以前肯定是可以正常获取的。那么极有可能是最近chrome更新完成之后导致!

碰到这种事只能啃文档了!!!

终于功夫不负有心人!!!

Content Scripts

Content scripts are JavaScript files that run in the context of web pages. By using the standard Document Object Model (DOM), they can read details of the web pages the browser visits, or make changes to them.

Here are some examples of what content scripts can do:

  • Find unlinked URLs in web pages and convert them into hyperlinks
  • Increase the font size to make text more legible
  • Find and process microformat data in the DOM

However, content scripts have some limitations. They cannot:

These limitations aren't as bad as they sound. Content scripts can indirectly use the chrome.* APIs, get access to extension data, and request extension actions by exchanging messages with their parent extension. Content scripts can also make cross-site XMLHttpRequests to the same sites as their parent extensions, and they cancommunicate with web pages using the shared DOM. For more insight into what content scripts can and can't do, learn about the execution environment.

原来runtime的权限是有限的!

我们在权限配置如下

"externally_connectable": {    "matches": [      "http://*.f6car.com/*",      "https://*.f6car.com/*",      "http://localhost:9001/*",      "http://127.0.0.1:9001/*",      "http://192.168.1.153:9001/*",      "http://*.web.f6car:9001/*",      "http://192.168.1.19:9001/*"    ]  },

因此猜测和这个配置有关。那么chrome在版本升级之后权限校验更加严格导致!

总结

大概是原先检测比较宽泛 api调用成功。但是随着版本升级 该代码权限校验更加严格导致部分api获取不到!

本文发表于2018年05月24日 00:00
(c)注:本文转载自https://my.oschina.net/qixiaobo025/blog/1817653,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 5280 讨论 0 喜欢 1

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1