【问题标题】:Chrome Extension - Injected Iframe not accessing Chrome browserAction or chrome.tabs or AngularJSChrome 扩展 - 注入的 iframe 未访问 Chrome browserAction 或 chrome.tabs 或 AngularJS
【发布时间】:2014-12-20 21:40:36
【问题描述】:

我有一个 chrome 扩展程序,可以通过点击浏览器操作来切换侧边栏。侧边栏包含一个带有本地(chrome 扩展)源的 iframe。我认为 iframe 中的页面将被视为本地 chrome 扩展文件,可以对 chrome API 等进行开放访问。但是,我在 web 控制台中不断收到以下错误:

Uncaught TypeError: Cannot read property 'onClicked' of undefined

TypeError: Cannot read property 'query' of undefined

如何让注入上下文脚本的 iframe 可以访问本地 chrome 环境?

代码如下:

sidebar.js:

var myApp = angular.module('PlaceMates', []);


myApp.service('pageInfoService', function() {
    this.getInfo = function(callback) {
        var model = {};

        chrome.tabs.query({
            'active': true,               // Select active tabs
            lastFocusedWindow: true     // In the current window
        },
        function (tabs) {
            if (tabs.length > 0)
            {
                model.title = tabs[0].title;
                model.url = tabs[0].url;

                chrome.tabs.sendMessage(tabs[0].id, { 'action': 'PageInfo' }, function (response) {
                    model.pageInfos = response;
                    console.log("popup js: " + model.pageInfos);
                    callback(model);
                });
            }

        });
    };
});

myApp.controller("PageController", function ($scope, pageInfoService) {
    $scope.message = "This extension identifies the photos on this page!";

    pageInfoService.getInfo(function (info) {
        $scope.title = info.title;
        $scope.url = info.url;
        $scope.pageInfos = info.pageInfos;
        $scope.place_name = info.place_name;
        $scope.$apply();
    });
});

background.js

console.log( 'Background.html starting!' );

// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
    // No tabs or host permissions needed!
    console.log('Toggling sidebar on ' + tab.url);

    // send message to current tab when clicked
    var tabId = tab.id;
    console.log("tab.id: " + tabId);
    chrome.tabs.query({active: true, currentWindow: true}, function(tab) {

        chrome.tabs.sendMessage(
            //Selected tab id
            tabId,
            //Params inside a object data
            {callFunction: "toggleSidebar"}, 
            //Optional callback function
            function(response) {
                console.log(response);
            }
        );
    });


    console.log('Done toggling sidebar!');

});

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
    if(request.cmd == "read_file") {
        $.ajax({
            url: chrome.extension.getURL("sidebar.html"),
            dataType: "html",
            success: sendResponse
        });
    }
})

chrome.runtime.onMessage.addListener(
    function(msg, sender, sendResponse) {
        console.log(sender.tab ?
                    "from a content script:" + sender.tab.url :
                    "from the extension");
        if (msg.command == "read_info"){
            console.log(JSON.parse(JSON.stringify(msg.myInfo)));
            sendResponse({command: "done"});
        }
    }
);

console.log( 'Background.html done.' );

【问题讨论】:

    标签: javascript angularjs iframe google-chrome-extension content-script


    【解决方案1】:

    唯一可以访问所有 chrome.* 扩展 API 的页面是在扩展源和 Chrome 扩展进程中运行的页面。

    当扩展页面嵌入到 iframe 中时,其扩展运行时相当于内容脚本:页面只能使用跨域 XMLHttpRequest 和一些扩展 API(messaging 和 @987654322 中的一些其他方法@/chrome.extension命名空间)。

    如果您希望其他 Chrome API 的功能可用于您的 iframe,那么您必须从 background page 调用这些 API,并使用 messaging API 将来自 iframe 的请求代理到后台页面和背部。幸运的是,大多数 Chrome 扩展 API 在设计上都是异步的,因此更改代码以使用这些代理并不难。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-12
    • 2015-02-28
    • 2012-10-27
    • 2016-10-09
    • 2012-07-08
    • 1970-01-01
    • 2017-10-22
    • 2012-08-15
    相关资源
    最近更新 更多