本文介绍了运行tabs.executeScript时未检查runtime.lastError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法构建了Ripple Emulator开源代码()。

我按照说明制作了它(Jake build),它们创建了Chrome扩展目标,允许我通过Chrome浏览器测试我的Web应用程序根据。



我成功将解压后的扩展加载到chrome中,但是当启用它时没有任何反应,虽然页面重新加载扩展不起作用,但我得到2个错误:
$ b

  • 未捕获的ReferenceError:webkitNotifications不是定义

      webkitNotifications.createHTMLNotification('/ views / update.html')。show(); 


  • 运行tabs.executeScript时未经检查的runtime.lastError:无法访问chrome:// URL

      chrome.tabs.executeScript(tabId,{


  • 如何解决此问题?




    Full background.js:

      if(!window.tinyHippos){
    window。 tinyHippos = {};
    }

    tinyHippos.Background =(function(){
    var _wasJustInstalled = false,
    _self;

    函数isLocalRequest(uri){
    return !! uri.match(/ ^ https::\ / \ /(127\.0\.0\.1 | localhost)| ^ file:\\ \\ / \ //);
    }

    函数initialize(){
    //检查显示欢迎/更新视图的版本信息
    var version = window。 localStorage [ripple-version],
    xhr = new window.XMLHttpRequest(),
    userAgent,
    requestUri = chrome.extension.getURL(manifest.json);

    _self .bindContextMenu();

    xhr.onreadystatechange = function(){
    if(xhr.readyState === 4){
    var manifest = JSON.parse(xhr.responseText ),
    currentVersion = manifest.version;

    if(!version){
    _wasJustInstalled = true;如果(version!== currentVersion){
    webkitNotifications.createHTMLNotification('/ views / update.html')。show();
    }

    if
    }

    window.localStorage [ripple-version] = currentVersion;
    }
    };

    xhr.open(GET,requestUri,true);

    xhr.send();

    chrome.extension.onRequest.addListener(function(request,sender,sendResponse){
    switch(request.action){
    caseisEnabled:
    console .log(isEnabled?==>+ request.tabURL);
    sendResponse({enabled:tinyHippos.Background.isEnabled(request.tabURL)});
    break;
    caseenable:
    console.log(enabled ==>+ request.tabURL);
    tinyHippos.Background.enable(); $ b $ sendResponse();
    break;
    caseversion:
    sendResponse({version:version});
    break;
    casexhr:
    var xhr = new XMLHttpRequest(),
    postData = new FormData(),
    data = JSON.parse(request.data);

    console.log(xhr ==>+ data.url);

    $ .ajax({
    类型:data.method,
    url:data .url,
    async:true,
    data:data.data,
    success:function(data,status){
    sendResponse({
    code:200,
    data:data
    });
    },
    error:function(xhr,status,errorMessage){
    sendResponse({
    code:xhr.status,
    data:status
    } );
    }
    });
    休息;
    案例userAgent:
    案例lag:
    案例network:
    //稍后实施的方法
    break;
    default:
    throw {name:MethodNotImplemented,消息:请求的操作不被支持!};
    休息;
    };
    });

    chrome.tabs.onUpdated.addListener(函数(tabId,changeInfo,tab){
    if(tinyHippos.Background.isEnabled(tab.url)){
    chrome.tabs .executeScript(tabId,{
    code:rippleExtensionId ='+ chrome.extension.getURL('')+';,
    allFrames:false
    },function(){
    chrome.tabs.executeScript(tabId,{
    file:bootstrap.js,
    allFrames:false
    });
    });
    }
    });


    函数_getEnabledURIs(){
    var parsed = localStorage [tinyhippos-enabled-uri];
    返回解析? JSON.parse(已解析):{};
    }

    函数_persistEnabled(url){
    var jsonObject = _getEnabledURIs();
    jsonObject [url.replace(/.[^ \ /] * $ /,)] =widget;
    localStorage [tinyhippos-enabled-uri] = JSON.stringify(jsonObject);

    $ b _self = {
    metaData:function(){
    return {
    justInstalled:_wasJustInstalled,
    version:window.localStorage [ ripple-version]
    };


    bindContextMenu:function(){
    var id = chrome.contextMenus.create({
    type:normal,
    title:Emulator
    });

    // TODO:现在破解(因为打开的选项卡被假定为从
    调用页面上下文//最终将能够在data.pageUrl中传递以启用/禁用持久性重构完成
    chrome.contextMenus.create({
    type:normal,
    title:Enable,
    contexts:[page ],
    parentId:id,
    onclick:function(data){
    _self.enable();
    }
    });

    chrome.contextMenus.create({
    type:normal,
    title:Disable,
    contexts:[page],
    parentId:id,
    onclick:function(data){
    _self.disable();
    }
    });
    } ,

    enable:function(){
    chrome.tabs.getSelected(null,function(tab){
    console.log(enab le ==> + tab.url);
    _persistEnabled(tab.url);
    chrome.tabs.sendRequest(tab.id,{action:enable,mode:widget, tabURL:tab.url});
    });
    },

    disable:function(){
    chrome.tabs.getSelected(null,function标签){
    console.log(disable ==>+ tab.url);

    var jsonObject = _getEnabledURIs(),$ b $ url = tab.url;

    while(url& url.length> 0){
    url = url.replace(/.[^ \ /] * $ /,);
    if(jsonObject [url]){
    delete jsonObject [url];
    break;
    }
    }

    localStorage [tinyhippos-enabled -uri] = JSON.stringify(jsonObject);

    chrome.tabs.sendRequest(tab.id,{action:disable,tabURL:tab.url});
    });
    },

    isEnabled:function(ur l,enabledURIs){
    if(url.match(/ enableripple = / i)){
    _persistEnabled(url);
    返回true;
    }

    // HACK:我确定有一种更好的方法来执行这个正则表达式
    if((url.match(/ ^ file:\ / \ / \ //)&& url.match(/ \ / + $ /))|| url.match(/(.*?)\。(jpg | jpeg | png | gif | css | js )$ /)){
    return false;
    }

    enabledURIs = enabledURIs || _getEnabledURIs();

    if(url.length === 0){
    return false;
    }
    else if(enabledURIs [url]){
    return true;
    }

    return tinyHippos.Background.isEnabled(url.replace(/.[^ \ /] * $ /,),enabledURIs);
    }
    };

    initialize();

    return _self;
    }());

    完整的manifest.json:



    <$ p $
    version:1,
    manifest_version:2,
    name:Ripple Emulator(Beta),
    背景:{
    page:views / background.html
    },
    web_accessible_resources:[],
    图标:{
    16:images / Icon_16x16.png,
    128:images / Icon_128x128.png,
    48:images / Icon_48x48.png
    },
    browser_action:{
    default_popup:views / popup.html,
    default_icon:images / Icon_48x48.png,
    default_title: ripple
    },
    content_scripts:[{
    run_at:document_start,
    js:[controllers / Insertion.js],
    matches:[http:// * / *,https:// * / *,file:// *]
    },
    {
    run_at:document_start,
    js:[controllers / frame.js],
    matches:[http:// * / *,https:// * / *,file:// *],
    all_frames:true
    }],
    permissions:[tabs,unlimitedStorage,notificatio ns,contextMenus,webRequest,< all_urls>],
    description:基于浏览器的html5移动应用程序开发和测试工具
    }


    解决方案

    这两个错误都很好地解释了这个问题:

    webkitNotifications未定义 - 这是因为webkitNotifications已被删除。我发现,它解释了正在发生的事情。



    无法访问chrome:// URL - 您必须尝试在chrome://网址上执行脚本(例如chrome:// settings,chrome :// extensions)这是非法的。为避免这样的错误打印,您可以使用
    - 检查chrome.runtime.lastError
    - 确保不要注入到chrome://页面。


    I managed to build Ripple Emulator open source (https://github.com/apache/incubator-ripple).

    I built it per the instructions (Jake build), which create the Chrome Extension Target that allows me to test my web apps via a chrome extension of my built, as per https://github.com/apache/incubator-ripple/blob/master/doc/chrome_extension.md.

    I successfully loaded the unpacked extension onto chrome, but when I enable it nothing happens, though the page reloads the extension does not work, instead I get 2 errors:

    1. Uncaught ReferenceError: webkitNotifications is not defined

      webkitNotifications.createHTMLNotification('/views/update.html').show();
      

    2. Unchecked runtime.lastError while running tabs.executeScript: Cannot access a chrome:// URL

      chrome.tabs.executeScript(tabId, {
      

    How do I solve this problem?


    Full background.js:

    if (!window.tinyHippos) {
    window.tinyHippos = {};
    }
    
    tinyHippos.Background = (function () {
    var _wasJustInstalled = false,
        _self;
    
    function isLocalRequest(uri) {
        return !!uri.match(/^https?:\/\/(127\.0\.0\.1|localhost)|^file:\/\//);
    }
    
    function initialize() {
        // check version info for showing welcome/update views
        var version = window.localStorage["ripple-version"],
            xhr = new window.XMLHttpRequest(),
            userAgent,
            requestUri = chrome.extension.getURL("manifest.json");
    
        _self.bindContextMenu();
    
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                var manifest = JSON.parse(xhr.responseText),
                    currentVersion = manifest.version;
    
                if (!version) {
                    _wasJustInstalled = true;
                }
    
                if (version !== currentVersion) {
                    webkitNotifications.createHTMLNotification('/views/update.html').show();
                }
    
                window.localStorage["ripple-version"] = currentVersion;
            }
        };
    
        xhr.open("GET", requestUri, true);
    
        xhr.send();
    
        chrome.extension.onRequest.addListener(function (request, sender, sendResponse) {
            switch (request.action) {
            case "isEnabled":
                console.log("isEnabled? ==> " + request.tabURL);
                sendResponse({"enabled": tinyHippos.Background.isEnabled(request.tabURL)});
                break;
            case "enable":
                console.log("enabling ==> " + request.tabURL);
                tinyHippos.Background.enable();
                sendResponse();
                break;
            case "version":
                sendResponse({"version": version});
                break;
            case "xhr":
                var xhr = new XMLHttpRequest(),
                    postData = new FormData(),
                    data = JSON.parse(request.data);
    
                console.log("xhr ==> " + data.url);
    
                $.ajax({
                    type: data.method,
                    url: data.url,
                    async: true,
                    data: data.data,
                    success: function (data, status) {
                        sendResponse({
                            code: 200,
                            data: data
                        });
                    },
                    error: function (xhr, status, errorMessage) {
                        sendResponse({
                            code: xhr.status,
                            data: status
                        });
                    }
                });
                break;
            case "userAgent":
            case "lag":
            case "network":
                // methods to be implemented at a later date
                break;
            default:
                throw {name: "MethodNotImplemented", message: "Requested action is not supported!"};
                break;
            };
        });
    
        chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
            if (tinyHippos.Background.isEnabled(tab.url)) {
                chrome.tabs.executeScript(tabId, {
                    code: "rippleExtensionId = '" + chrome.extension.getURL('') + "';",
                    allFrames: false
                }, function () {
                    chrome.tabs.executeScript(tabId, {
                        file: "bootstrap.js",
                        allFrames: false
                    });
                });
            }
        });
    }
    
    function _getEnabledURIs() {
        var parsed = localStorage["tinyhippos-enabled-uri"];
        return parsed ? JSON.parse(parsed) : {};
    }
    
    function _persistEnabled(url) {
        var jsonObject = _getEnabledURIs();
        jsonObject[url.replace(/.[^\/]*$/, "")] = "widget";
        localStorage["tinyhippos-enabled-uri"] = JSON.stringify(jsonObject);
    }
    
    _self = {
        metaData: function () {
            return {
                justInstalled: _wasJustInstalled,
                version: window.localStorage["ripple-version"]
            };
        },
    
        bindContextMenu: function () {
            var id = chrome.contextMenus.create({
                "type": "normal",
                "title": "Emulator"
            });
    
            // TODO: hack for now (since opened tab is assumed to be page context was called from
            // eventually will be able to pass in data.pageUrl to enable/disable when persistence     refactor is done
            chrome.contextMenus.create({
                "type": "normal",
                "title": "Enable",
                "contexts": ["page"],
                "parentId": id,
                "onclick": function (data) {
                        _self.enable();
                    }
            });
    
            chrome.contextMenus.create({
                "type": "normal",
                "title": "Disable",
                "contexts": ["page"],
                "parentId": id,
                "onclick": function (data) {
                        _self.disable();
                    }
            });
        },
    
        enable: function () {
            chrome.tabs.getSelected(null, function (tab) {
                console.log("enable ==> " + tab.url);
                _persistEnabled(tab.url);
                chrome.tabs.sendRequest(tab.id, {"action": "enable", "mode": "widget", "tabURL": tab.url });
            });
        },
    
        disable: function () {
            chrome.tabs.getSelected(null, function (tab) {
                console.log("disable ==> " + tab.url);
    
                var jsonObject = _getEnabledURIs(),
                    url = tab.url;
    
                while (url && url.length > 0) {
                    url = url.replace(/.[^\/]*$/, "");
                    if (jsonObject[url]) {
                        delete jsonObject[url];
                        break;
                    }
                }
    
                localStorage["tinyhippos-enabled-uri"] = JSON.stringify(jsonObject);
    
                chrome.tabs.sendRequest(tab.id, {"action": "disable", "tabURL": tab.url });
            });
        },
    
        isEnabled: function (url, enabledURIs) {
            if (url.match(/enableripple=/i)) {
                _persistEnabled(url);
                return true;
            }
    
                // HACK: I'm sure there's a WAY better way to do this regex
            if ((url.match(/^file:\/\/\//) && url.match(/\/+$/)) || url.match(/(.*?)\.        (jpg|jpeg|png|gif|css|js)$/)) {
                return false;
            }
    
            enabledURIs = enabledURIs || _getEnabledURIs();
    
            if (url.length === 0) {
                return false;
            }
            else if (enabledURIs[url]) {
                return true;
            }
    
              return tinyHippos.Background.isEnabled(url.replace(/.[^\/]*$/, ""), enabledURIs);
        }
       };
    
        initialize();
    
        return _self;
    }());
    

    Full manifest.json:

    {
    "version": "1",
    "manifest_version": 2,
    "name": "Ripple Emulator (Beta)",
    "background": {
        "page": "views/background.html"
    },
    "web_accessible_resources": [],
    "icons":{
        "16":"images/Icon_16x16.png",
        "128":"images/Icon_128x128.png",
        "48":"images/Icon_48x48.png"
    },
    "browser_action":{
        "default_popup":"views/popup.html",
        "default_icon":"images/Icon_48x48.png",
        "default_title":"Ripple"
    },
    "content_scripts":[{
        "run_at": "document_start",
        "js": ["controllers/Insertion.js"],
        "matches": ["http://*/*","https://*/*","file://*"]
    },
    {
        "run_at": "document_start",
        "js": ["controllers/frame.js"],
        "matches": ["http://*/*","https://*/*","file://*"],
        "all_frames": true
    }],
    "permissions": ["tabs", "unlimitedStorage", "notifications", "contextMenus", "webRequest", "<all_urls>"],
    "description": "A browser based html5 mobile application development and testing tool"
    }
    
    解决方案

    Both of these errors explain the problem well:

    "webkitNotifications is not defined" - this is because webkitNotifications has been removed. I found Chrome webkitNotification not found : api missing which explains what is going on.

    "Cannot access a chrome:// URL" - you must be trying to execute the script on a chrome:// URL (like chrome://settings, chrome://extensions) which is illegal. To avoid the error printing like that, you can either- check chrome.runtime.lastError- make sure not to inject onto chrome:// pages.

    这篇关于运行tabs.executeScript时未检查runtime.lastError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-06 02:23