标签: content-script

Chrome扩展程序将外部JavaScript添加到当前页面的HTML

我通过Chrome扩展程序在页面末尾添加了一些外部JavaScript.然后外部JavaScript尝试将一些数据发布回服务器,但是没有发生.

JavaScript希望获取当前页面和引用者的URL并将其发回服务器.

任何人都可以请教我这里有什么问题,如果不可能这样我怎样才能将数据从当前页面发回服务器.

的manifest.json

{
  "name": "Website Safety",
  "version": "1.0",
  "manifest_version": 2,
  "description": "The Website Safety Extension.",
  "browser_action": {
    "name": "View the website information for ",
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "tabs", "http://*/*", "https://*/*"
  ],
  "background": {
  //  "scripts": ["refresh.js"]
    "page": "background.html"
  },
  "content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'",
  //"background_page": "background.html"

  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["contentScript.js"]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

现在是contentScript.js

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-31046309-1']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = …
Run Code Online (Sandbox Code Playgroud)

javascript dom google-analytics google-chrome-extension content-script

24
推荐指数
3
解决办法
3万
查看次数

通过Google Chrome中的executeScript注入多个脚本

我需要以编程方式从我的Google Chrome扩展程序中将多个脚本文件(后跟代码段)注入当前页面.该chrome.tabs.executeScript方法允许单个InjectDetails对象(表示脚本文件或代码片段),以及在脚本之后执行的回调函数.目前的答案提出嵌套executeScript调用:

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(null, { file: "jquery.js" }, function() {
        chrome.tabs.executeScript(null, { file: "master.js" }, function() {
            chrome.tabs.executeScript(null, { file: "helper.js" }, function() {
                chrome.tabs.executeScript(null, { code: "transformPage();" })
            })
        })
    })
});
Run Code Online (Sandbox Code Playgroud)

但是,回调嵌套变得难以处理.有没有办法抽象这个?

javascript google-chrome google-chrome-extension content-script

24
推荐指数
2
解决办法
1万
查看次数

chrome扩展中的后台脚本和内容脚本之间的区别

正如问题所说,我只想知道chrome扩展中后台脚本和内容脚本之间区别.当我在两个脚本中记录chrome对象时,我发现了不同的对象.

用例

我想在点击图标时将我的javascript注入页面所以在manifest.json我添加内容脚本但我无法在内容脚本中收听图标点击事件.

chrome.browserAction未在内容脚本中的chrome对象中定义.

如何在内容脚本中侦听单击事件.我们可以同时包含背景和内容脚本吗?

这是我的manifest.json

{
  "name": "First Plugin Testing",    
  "version": "1.0",
  "manifest_version": 2,    
  "description": "Trying hands on first extension",
  "background": { "scripts": ["background.js"] },
  "browser_action": {
    "default_icon": "icon.png"
  },
  "permissions": [
    "tabs", "http://*/*", "https://*/*"
  ],
  "content_scripts": [
    {
      "matches": ["http://*/*"], 
      "js": ["temp.js"]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

google-chrome google-chrome-extension content-script

23
推荐指数
1
解决办法
1万
查看次数

从内容脚本访问窗口变量

我有一个Chrome扩展程序,如果window.my_variable_name存在变量,它会尝试查找每个浏览过的网址(以及每个浏览器网址的每个iframe).

所以我写了一小段内容脚本:

function detectVariable(){
    if(window.my_variable_name || typeof my_variable_name !== "undefined") return true;
    return false;
}
Run Code Online (Sandbox Code Playgroud)

在尝试太久之后,似乎Content Scripts在一些沙箱中运行.

有没有办法window从Chrome内容脚本访问该元素?

sandbox google-chrome-extension content-script

22
推荐指数
1
解决办法
2万
查看次数

Chrome扩展程序从Background.js向内容脚本发送消息

我已经阅读了有关如何从后台javascript文件(main.js)发送消息到内容脚本(content.js)的文档,但我无法让onMessage打开我的警报.

的manifest.json

{
   "name": "Example",
   "version": "1.0.1",
   "manifest_version" : 2,
   "description": "Example Description",
   "background" : {
     "scripts" : ["main.js"]
   },
   "page_action" : {
      "default_icon": {
         "19": "icons/19.png",
         "38": "icons/38.png"
      },
      "default_title" : "Example Title"
   },
   "content_scripts": [{
      "matches": ["<all_urls>"],
      "js": ["lib/jquery-1.8.3.min.js","scripts/content.js"],
      "run_at": "document_idle",
      "all_frames": false
   }],
   "permissions": [
       "tabs",
       "geolocation"
   ],
   "icons": {
       "16": "icons/16.png",
       "48": "icons/48.png",
       "128": "icons/48.png"
   }

}
Run Code Online (Sandbox Code Playgroud)

后台javascript文件(main.js)

chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
    chrome.tabs.sendMessage(tabs[0].id, {action: "SendIt"}, function(response) {});  
});
Run Code Online (Sandbox Code Playgroud)

内容javascript文件(content.js)

chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) { …
Run Code Online (Sandbox Code Playgroud)

manifest google-chrome-extension content-script

19
推荐指数
1
解决办法
1万
查看次数

我的CSS没有通过我的内容脚本注入

任何人都可以向我解释这个.我正在尝试使用带有Google扩展的content_script将CSS文件注入网页,但我的css文件永远不会添加到网页中.有人能告诉我我做错了什么并帮我解决了吗?谢谢

表现:

{
  "name": "Extension",
  "version": "0",
  "description": "",


  "permissions": ["tabs", "http://*/*", "https://*/*", "file:///*/*"],
    "content_scripts": [
    {
        "matches": [ "http://*/*", "https://*/*", "file:///*/*"],
        "css": ["myStyles.css"],
        "js": ["myScript.js"],
        "all_frames": true
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

myStyles.css

#test {
    margin: 0 10px;
    background: #fff;
    padding: 3px;
    color: #000;
}
Run Code Online (Sandbox Code Playgroud)

css google-chrome-extension content-script

17
推荐指数
1
解决办法
2万
查看次数

使用Chrome内容脚本添加复杂的HTML

我正在使用Chrome扩展程序的内容脚本来创建添加到网页上的复杂显示.

我首先将它直接集成在一个网站上进行测试,但现在我需要把它放在一个扩展中.

问题是Chrome的内容脚本API只允许注入javascript.这意味着,为了注入复杂的HTML布局,我需要完全用JS对象来编写它,这很难编写,难以维护并且绝对不是设计人员友好的.

我想知道是否有人知道或者可以想到一个聪明的方法来获得更好的工作流程.

javascript html5 google-chrome google-chrome-extension content-script

17
推荐指数
2
解决办法
1万
查看次数

Chrome扩展程序:内容脚本和background.html之间的通信

我是Chrome扩展新手.我正在尝试在内容脚本和background.html页面之间进行通信.该background.html发送一个请求," 你好 ",以内容脚本和内容脚本应该以"进行回应你好背景 "警报.但它只是没有发生.我的background.html代码是:

function testRequest() {        
    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.sendRequest(tab.id, {greeting: "hello"});
    });    
}
Run Code Online (Sandbox Code Playgroud)

content.js代码:

chrome.extension.onMessage.addListener(
    function(request, sender, sendResponse) {
        if (request.greeting == "hello")
        alert("hello background");
    }
);
Run Code Online (Sandbox Code Playgroud)

popup.html代码:

<!doctype html>
<html>
    <head></head>
    <body>
        <form>
            <input type="button" value="sendMessage" onclick="testRequest()" />
        </form>    
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

manifest.json:

{
    "browser_action": {
        "default_icon": "icon.png",
        "popup": "popup.html"
    },
    "background": {
        "page": "background.html"
    },
    "permissions": [
        "tabs",
        "http://*/*",
        "https://*/*",
        "notifications",
        "contextMenus"
    ],
    "content_scripts": [
        {
            "matches": …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome-extension content-script

16
推荐指数
1
解决办法
2万
查看次数

什么是chrome检查器中的内容脚本

虽然这可能是一个非常基本的事情,但我似乎找不到这个问题的答案.我看到很多讨论content scripts.当我打开web inspectorchrome,选择sources,我看到一个叫做标签content scripts.我看到几个随机数和一些脚本,我似乎无法弄清楚它们是什么.这些如何显示?这些来自哪里?我看不到我的服务器提供任何这些服务.

google-chrome web-inspector content-script

16
推荐指数
1
解决办法
6234
查看次数

可选择注入内容脚本

可以通过在扩展清单文件中声明来以编程方式或永久方式注入内容脚本.程序注入需要主机权限,通常由浏览器或页面操作授予.

在我的用例中,我想在没有用户操作的情况下注入gmail,outlook.com和yahoo邮件网站.我可以通过声明所有这些显示来做,但这样做需要所有数据访问这些帐户.某些用途可能只想授予outlook.com,但不授予gmail.程序注射不起作用,因为我需要知道何时注射.使用制表符权限也需要另一个权限.

有没有什么好方法可以选择注入网站?

google-chrome-extension content-script

16
推荐指数
2
解决办法
2880
查看次数