zoukankan      html  css  js  c++  java
  • 一起来做Chrome Extension《一些问题》

    目录

    1. Unchecked runtime.lastError: The message port closed before a response wa received.
    2. 使用 eval
    3. Content script注入iframe
      1. Extenstion内的html
      2. 站外连接

    1. Unchecked runtime.lastError: The message port closed before a response wa received.

    此错误一般发生在background js和content js通讯的时候,问题描述得也非常清楚,解决方法非常简单,即在收到消息后,在同步时间里send respnose就可以了。

    注意:send response如果在异步方法里,并不能解决这个问题。

    // Example:
    // background js 或content js
    chrome.extension.onMessage.addListener(function(request, _, sendResponse) {
        sendResponse('');
    });
    

    2. 使用eval

    Chrome Extension默认是禁止使用eval方法的,使用之前,需要先在manifest.json里开启,如下:

    "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
    

    3. 使用iframe

    注入iframe同样受content_security_policy限制,而且会受到目标站点的content_security_policy限制。关于content_security_policy内容比较多,这里分成两种情况

    3.1 Extension内的html

    注入的iframe加载Extension内的html稍微没有这么麻烦,只需要在manifest.json里指定要加载的html就好了

    "web_accessible_resources": ["example.html"]
    

    注入iframe的src,可以使用chrome.runtime.getUrl()来获取地址

    let src = chrome.runtime.getURL('example.html')
    

    注:要注入网站的content_security_policy对这样的iframe注入会不会有影响,目前还没有测试到。

    此方法在 Fika (reader-mode) 扩展里有使用

    3.2 站外连接

    如上所说,注入iframe是受目标网站的content_security_policy限制的,所以,如果目标网站不允许,你的注入将会失败,如medium.com的content_security_policy关于frame-src的部分:

    default-src 'self';

    ...

    frame-src chromenull: https: webviewprogressproxy: medium: 'self';

    ...

    它允许了https的地址,所以,注入的iframe加载https地址是没有问题的,而http的地址将被拒绝。因为注入已经离开了Chrome Extenstion的范围,所以,不管你怎么对Chrome Extension的content_security_policy进行设置并不会有用。

    关于content_security_policy,可以看 https://www.html5rocks.com/en/tutorials/security/content-security-policy/

  • 相关阅读:
    Longest Palindromic Substring
    PayPal MLSE job description
    Continuous Median
    Remove Duplicates From Linked List
    Valid IP Address
    Longest substring without duplication
    Largest range
    Subarray sort
    Multi String Search
    Suffix Trie Construction
  • 原文地址:https://www.cnblogs.com/onlyfu/p/10443114.html
Copyright © 2011-2022 走看看