简体   繁体   English

从页面DOM访问NPAPI

[英]Access NPAPI from pages DOM

I'm attempting to override the default functionality for webkitNotifications.createNotification and via a Chrome extension I'm able to inject a script in the pages DOM that does so. 我试图覆盖webkitNotifications.createNotification的默认功能,并通过Chrome扩展程序能够在脚本页面DOM中插入脚本。 Problem I'm having now is I need access to chrome.extension.sendRequest from the pages DOM in order to push my request to the NPAPI I have embedded in the background page. 我现在遇到的问题是,我需要从页面DOM访问chrome.extension.sendRequest ,以便将我的请求推送到我已嵌入后台页面的NPAPI中。 I previously had the embed object rendered on each page during the execution of the content-script - but believe it's more effective (and safe) if the NPAPI is embedded within the extension not injected on every page. 我以前在执行内容脚本期间在每个页面上都渲染了embed对象-但是相信,如果NPAPI嵌入在扩展名中而不是在每个页面中都没有嵌入,它会更有效(更安全)。

if (window.webkitNotifications)
{
    (function()
    {
        window.webkitNotifications.originalCreateNotification = window.webkitNotifications.createNotification;
        window.webkitNotifications.createNotification = function (iconUrl, title, body) {
            var n = window.webkitNotifications.originalCreateNotification(iconUrl, title, body);
            n.original_show = n.show;
            n.show = function ()
            {
                console.log("Chrome object", chrome);
                console.log("Chrome.extension object", chrome.extension);
                chrome.extension.sendRequest({'title' : title, 'body' : body, 'icon' : iconUrl});
            }
            return n;
        }
    })();
}

That is what is injected in the DOM as a script element. 那就是作为脚本元素注入DOM的内容。 The background page is as follows: 后台页面如下:

<embed type="application/x-npapiplugz" id="plugz">
<script>
var osd = document.getElementById('plugz');

function processReq(req, sender, callback)
{
    osd.notify(req.title, req.body, req.image);

    console.log("NOTIFY!", req.title, req.body, req.image);
};

chrome.extension.onRequest.addListener(processReq);
</script>

Once your extension includes a NPAPI plugin, it is no longer safe :) But your correct, instead of allowing every single page have access to the plugin, it is better to let your extension have it. 一旦您的扩展程序包含NPAPI插件,它便不再安全:)但是您的正确做法不是让每个页面都可以访问该插件,而是让您的扩展程序拥有它是更好的选择。 I assume you know about the "public" property which specifies whether your plugin can be accessed by regular web pages, the default is false. 我假设您知道“ public”属性,该属性指定常规网页是否可以访问您的插件,默认值为false。

Below, I will explain whats your problem, it isn't a accessing NPAPI from DOM pages problem, it is basically why can't your notifications access your content script or extension pages. 下面,我将解释您的问题,这不是从DOM页面访问NPAPI的问题,基本上是为什么通知无法访问您的内容脚本或扩展页面的原因。

As you noticed, access to the content scripts and the pages DOM are isolated from each other. 如您所见,对内容脚本的访问和页面DOM相互隔离。 The only thing they share, is the DOM. 他们唯一共享的是DOM。 If you want your notifications override to communicate to your content script, you must do so within a shared DOM. 如果希望您的通知覆盖以与您的内容脚本进行通信,则必须在共享DOM中进行通信。 This is explained in Communication with the embedding page in the Content Scripts documentation. Content Scripts文档中的与嵌入页面进行通信中对此进行了说明。

You could do it the event way, where your content script listens on such event for data coming from your DOM, something like the following: 您可以通过事件方式进行操作,其中内容脚本在此类事件中侦听来自DOM的数据,如下所示:

var exportEvent = document.createEvent('Event');
exportEvent.initEvent('notificationCallback', true, true);
window.webkitNotifications.createNotification = function (iconUrl, title, body) {
  var n = window.webkitNotifications.createNotification(iconUrl, title, body);
  n.show = function() {
    var data = JSON.stringify({title: title, body: body, icon: iconUrl});
    document.getElementById('transfer-dom-area').innerText = data;
    window.dispatchEvent(exportEvent);
  };
  return n;
}
window.webkitNotifications.createHTMLNotification = function (url) {
  var n = window.webkitNotifications.createHTMLNotification(url);
  n.show = function() {
    var data = JSON.stringify({'url' : url});
    document.getElementById('transfer-dom-area').innerText = data;
    window.dispatchEvent(exportEvent);
  };
  return n;
};

Then your event listener can send that to the background page: 然后,您的事件监听器可以将其发送到后台页面:

// Listen for that notification callback from your content script.
window.addEventListener('notificationCallback', function(e) {
  var transferObject = JSON.parse(transferDOM.innerText);
  chrome.extension.sendRequest({NotificationCallback: transferObject});
});

I added that to my gist on GitHub for the whole extension ( https://gist.github.com/771033 ), Within your background page, you can call your NPAPI plugin. 我在整个扩展( https://gist.github.com/771033 )的GitHub上都添加了该代码,在您的背景页面中,您可以调用NPAPI插件。

I hope that helps you out, I smell a neat idea from this :) 希望对您有帮助,我从中得到了一个很好的主意:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM