相关疑难解决方法(0)

在Google Chrome扩展程序中加载外部JavaScript

我正在编写一个操纵当前页面的Google Chrome扩展程序(基本上添加了一个按钮).

在我的内容脚本中,我想加载Facebook Graph API:

$fbDiv = $(document.createElement('div')).attr('id', 'fb-root');
$fbScript = $(document.createElement('script')).attr('src', 'https://connect.facebook.net/en_US/all.js');
$(body).append($fbDiv);
$(body).append($fbScript);

console.log("fbScript: " + typeof $fbScript.get(0));
console.log("fbScript parent: " + typeof $fbScript.parent().get(0));
console.log("find through body: " + typeof $(body).find($fbScript.get(0)).get(0));
Run Code Online (Sandbox Code Playgroud)

但是,脚本似乎没有添加到body.这是控制台日志:

fbScript: object
fbScript parent: undefined
find through body: undefined
Run Code Online (Sandbox Code Playgroud)

关于我做错了什么的任何想法?

javascript google-chrome google-chrome-extension facebook-graph-api

25
推荐指数
2
解决办法
4万
查看次数

为什么chrome.tabs.executeScript()需要更改当前网站DOM以及如何使用jQuery来实现相同的效果?

更新:我最近收到了很多负面消息,认为这是一个非常古老的问题(2.5年).我甚至不再使用jquery了.所以请休息一下!


我之前已经制作了一些chrome扩展程序,但这是我第一次需要在网站上更改用户当前正在查看的内容,以响应扩展弹出窗口中的按钮点击.

正如我意识到只是执行一些jQuery行无效.什么有效是我在谷歌开发者页面示例扩展中找到的方法: chrome.tabs.executeScript()但我不知道为什么它是必要的.

必须有一些我不知道的基本概念.有谁可以向我解释一下?我可以执行jQuery(加载)吗?

完整示例:

$('#change_button').on('click', function() {

  //this doesen't work
  $("body").css({backgroundColor: 'blue'});

  //but this line does
  chrome.tabs.executeScript(null, {code:"document.body.style.backgroundColor='red'"});

});
Run Code Online (Sandbox Code Playgroud)

实际上我非常需要jQuery在DOM中做一些更改并对它们做出响应,即:

if( $(".list,.list-header-name").hasClass("tch"))
{
  $(".list,.list-header-name").addClass("tch");
}
Run Code Online (Sandbox Code Playgroud)

jquery google-chrome-extension

7
推荐指数
1
解决办法
2956
查看次数

运行tabs.executeScript时未经检查的runtime.lastError?

我设法构建了Ripple Emulator开源软件(https://github.com/apache/incubator-ripple).

我根据说明(Jake构建)构建了它,它创建了Chrome扩展目标,允许我通过我内置的chrome扩展测试我的网络应用程序,按照https://github.com/apache/incubator-ripple/blob /master/doc/chrome_extension.md.

我成功地将解压缩的扩展加载到chrome上,但是当我启用它时没有任何反应,虽然页面重新加载扩展不起作用,而是我得到2个错误:

  1. 未捕获的ReferenceError:未定义webkitNotifications

    webkitNotifications.createHTMLNotification('/views/update.html').show();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 运行tabs.executeScript时未经检查的runtime.lastError:无法访问chrome:// URL

    chrome.tabs.executeScript(tabId, {
    
    Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?


完整的background.js:

if (!window.tinyHippos) {
window.tinyHippos = {};
}

tinyHippos.Background = (function () {
var _wasJustInstalled = false,
    _self;

function isLocalRequest(uri) {
    return !!uri.match(/^https?:\/\/(127\.0\.0\.1|localhost)|^file:\/\//);
}

function initialize() {
    // check version info for showing welcome/update views
    var version = window.localStorage["ripple-version"],
        xhr = new window.XMLHttpRequest(),
        userAgent,
        requestUri = chrome.extension.getURL("manifest.json");

    _self.bindContextMenu();

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            var manifest = JSON.parse(xhr.responseText),
                currentVersion = …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome google-chrome-extension ripple

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