ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – 运行tabs.executeScript时未经检查的runtime.lastError?

2019-09-16 00:42:53  阅读:235  来源: 互联网

标签:ripple javascript google-chrome google-chrome-extension


我设法构建了Ripple Emulator开源(https://github.com/apache/incubator-ripple).

我根据说明(Jake构建)构建了它,它创建了Chrome扩展目标,允许我通过我的内置的chrome扩展来测试我的网络应用程序,如https://github.com/apache/incubator-ripple/blob/master/doc/chrome_extension.md所示.

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

>未捕获的ReferenceError:未定义webkitNotifications

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

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

chrome.tabs.executeScript(tabId, {

我该如何解决这个问题?

完整的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;
}());

完整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"
}

解决方法:

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

“webkitNotifications未定义” – 这是因为webkitNotifications已被删除.我发现Chrome webkitNotification not found : api missing解释了发生了什么.

“无法访问chrome:// URL” – 您必须尝试在chrome:// URL(如chrome:// settings,chrome:// extensions)上执行该脚本,这是非法的.为了避免像这样的错误打印,你也可以
– 检查chrome.runtime.lastError
– 确保不要注入chrome://页面.

标签:ripple,javascript,google-chrome,google-chrome-extension
来源: https://codeday.me/bug/20190916/1806530.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有