【问题标题】:How to read a js script file from cjs chrome extension如何从 cjs chrome 扩展中读取 js 脚本文件
【发布时间】:2016-04-19 07:32:45
【问题描述】:

我安装了 Google chrome 扩展程序 cjs(用于网站的自定义 JavaScript)。

如果我在 cjs 编辑器中编写脚本,该脚本工作正常:

我将脚本写在一个 js 文件中,并将其放入我的 IIS 中,如下所示:

  • 打开 IIS 管理器
  • 右键单击默认网站
  • 添加应用程序
  • 别名 = googleScript
  • 物理路径:C:\scripts\google(例如)
  • 我创建了一个 googleScript.js 文件并将其保存在 C:\scripts\google 中。文件内容为:alert("Google");
  • 我点击了Connect As并选择应用程序用户(直通身份验证)
  • 我打开了位于 googleScript(在 IIS 中)中的 默认文档,并添加了一个名为:googleScript.js 的新默认文档
  • 当我右键单击 googleScript 并选择 Manage Application->Browse 时,正在使用我编写的脚本打开一个新网页。页面的url是http://localhost/googleScript/
  • 现在回到 cjs 扩展,而不是 alert("Google"); 我写的是 //localhost/googleScript/googleScript.js;

调用脚本文件时,警报不再起作用。从 cjs chrome 扩展调用脚本文件的正确方法是什么?

【问题讨论】:

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


    【解决方案1】:

    阅读

    在继续之前...


    我创建了一个小的weave,您可以用它来为您的应用程序/扩展试验这个想法。

    现在这是在线获取和保存外部脚本源的一种方法。

    var download_to_textbox = function (url, el) {
          return $.get(url, null, function (data) {
            el.val(data)
          }, "text")
        };
    

    获取脚本源代码并将其添加到文本区域

    download_to_textbox('libraries/alertifyjs/css/alertify.min.css', $('.alertifyjs1'));
    

    使用 FileSaver.js 保存文件 - https://github.com/eligrey/FileSaver.js/

    var blob = new Blob([ $('.alertifyjs1').val() ], {type: "text/html"});
    saveAs(blob, "alertify.min.css");
    

    【讨论】:

      【解决方案2】:

      这是我在 Chrome 85 上使用 Custom JavaScript for Websites 2, version 3.4.5 的方法。永久这样做不好,Chrome 团队有充分的理由禁用对 localhost 的不安全请求。

      1. 启用 chrome://flags/#allow-insecure-localhost(将其粘贴到 URL 栏中)

      2. http://localhost 添加到 chrome://flags/#unsafely-treat-insecure-origin-as-secure 并启用它

      3. 重启 Chrome

      4. 在终端窗口中,创建一个包含您的 javascript 文件的目录(假设它名为 my-script-file.js

      5. Serve that directory by cd'ing 进入并运行 python3 -m http.server

      6. 将此行添加到 cjs:

      //localhost:8000/my-script-file.js
      

      python3 -m http.server 默认服务于 8000 端口)

      1. 点击 CJS 弹出窗口左上角的“保存”

      您现在应该在 Chrome DevTools 的“网络”选项卡中看到对 //localhost:8000/my-script-file.js 的请求。

      【讨论】:

        猜你喜欢
        • 2015-05-05
        • 1970-01-01
        • 2013-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-19
        • 2021-10-19
        相关资源
        最近更新 更多