Chrome扩展/Bootstrap本地函数未定义?(Javascript)

3

看起来我无法在我的脚本中访问.popover方法。除非我搞砸了,否则我应该可以从包含的Bootstrap Native(无jQuery Bootstrap)文件中访问该方法。

脚本所做的所有事情就是添加链接和理想情况下在该元素上弹出一个气泡。

这是我的代码:

清单:

{
  "name": "foo",
  "description": "Work in Progress",
  "manifest_version": 2,
  "version": "0.8",
  "icons": { 
    "16": "icon16.png",
    "48": "icon48.png",
    "128": "icon128.png" 
  },
  "permissions": [
    "activeTab",
    "http://*/",
    "https://*/"
  ],
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "browser_action": {
    "default_title": "Click me!"
  }
}

背景:(在图标点击时运行)

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(tab.id, {file: "bootstrap-native.js" }, function() {
        chrome.tabs.executeScript(tab.id, {file: "content_script.js"});
});

});

content_script.js:

// Handle page's frame (to allow DOM access)
var page = top.frames["TargetContent"].document;

// Reference every professor listed and modify the registration page
Array.from(page.querySelectorAll("[id^='MTG_INSTR$']") ).forEach( el => {
    if (el.textContent == "Staff") {
        return;
    }

    // For every professor found, search for RMP page
    searchProfessor(el)

});



/**
 * Search for professor on RMP, then pass along to pageCheck
 * 
 * @param {Reference to prof} professorEl 
 */
function searchProfessor(professorEl) {
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            pageCheck(this.response,professorEl);

        }
    }

    // Search RMP using CSUF + Prof Name 
    xhr.open('GET', 'https://www.ratemyprofessors.com/search.jsp?queryoption=HEADER&queryBy=teacherName&schoolName=california+state+university+fullerton&schoolID=&query=' + professorEl.textContent +'&_action_search=Search');
    xhr.responseType = 'document';
    xhr.send();
}



/**
 * Verify prof's page exists and modify registration page
 * 
 * @param {DOM Obj} page 
 * @param {Reference to prof} element 
 */
function pageCheck(page,element){

    var ProfURL = page.getElementsByClassName('listing PROFESSOR')[0].childNodes[1].href

    // If the element exists, we have a hit (and the prof's page!)
    if (ProfURL) {
        // Link to the prof's RMP page
        addAnchor(element, ProfURL);    

        // Access the prof's specific RMP page
        var xhr1 = new XMLHttpRequest();

        // Create box to display prof info on hover
        xhr1.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                addTooltip(this.response,element);
            }
        }

        xhr1.open('GET', ProfURL);
        xhr1.responseType = 'document';
        xhr1.send();

    }

}

function addTooltip(profPage,profElement) {

    profElement.popover({title: "Header", content: "Blabla", trigger: "click"}); 
    var name = profElement.textContent;
    var grade = profPage.getElementsByClassName('grade')[0].textContent;    
    var difficulty = profPage.getElementsByClassName('grade')[2].textContent;
    var ratings = profPage.getElementsByClassName('table-toggle rating-count active')[0].textContent;
    ratings = ratings.trim();
}



/**
 * Assign hyperlink to element 
 * 
 * @param {Element} wrapper 
 * @param {String} URL 
 */
function addAnchor (wrapper, URL) {

    var a = document.createElement('a');
    a.href = URL;
    a.textContent = wrapper.textContent;

    // Opens in new window/tab
    a.setAttribute('target', '_blank');
    wrapper.replaceChild(a, wrapper.firstChild);
}

Bootstrap Native 的链接:

https://github.com/thednp/bootstrap.native

http://thednp.github.io/bootstrap.native/

错误:

content_script.js:75 Uncaught TypeError: profElement.popover is not a function
    at addTooltip (content_script.js:75)
    at XMLHttpRequest.xhr1.onreadystatechange (content_script.js:61)

bootstrap-native 是你从他们的 bootstrap native/dist/ 文件夹下载的 68kb 文件。我认为这可能是问题所在,因为当我将一个变量放入这个文件中时,它可以在内容脚本中访问,但不能在方法中访问。

对于这一切我都是新手,也许我拥有的 bootstrap native 文件不是正确的文件。或者我没有正确调用方法,但这不应该导致我出现这个错误。


@thednp 嘿!测试页面是我学校的注册页面。它很长,所以我会将其作为新评论。在那里,我只需选择任何课程标准以显示(我使用人类学系+恰好101),一旦您获得结果,请单击扩展图标,然后脚本就会运行。(其他评论继续回复) - Kevin
该页面应该发生什么? - thednp
@thednp 嘿,抱歉我几天没有回复你。学校的事情。关于主题-页面上没有提到bootstrap.native。我以为我已经将它包含在我的Chrome扩展程序中了。我需要将其注入到页面中吗?页面所做的事情并不重要;我似乎无法使用任何bootstrap函数。(请不要介意解释)。我正确地调用它们,但就像是C++一样,我忘记了我的#includes。你绝对正确,没有提到它。那个页面也不是我的。如果我确实需要在页面中注入它,请告诉我! - Kevin
是的,请确保在某种方式下在 content_script.js 之前包含库,可以通过 Chrome 扩展 API、require 或您选择的任何其他方式实现。 - thednp
显示剩余4条评论
1个回答

0

基于这个主题, 确保将jQuery和Bootstrap依赖项添加到您的清单文件中。

当您调用方法popover时,可能Bootstrap脚本尚未加载。

您有两个选项:

  1. $(window).load上调用该方法- http://output.jsbin.com/qoteqe
  2. 将Bootstrap和jQuery引用包含在DOM中,而不是在脚本中加载它们:http://output.jsbin.com/tamoda

这里有一些相关的帖子可能会有所帮助:


依赖关系应该包含在Bootstrap Native中,这样就可以避免使用jQuery。我认为不需要使用那个库添加Bootstrap依赖项,但是文档很差,所以我不确定。我现在正在旅行,但如果我无法让本地库工作,我会尝试使用jQuery。如果该方法未加载,我的VS Code IDE(IDE我猜?)至少会自动填充函数名称吧?这让我想到库可能是问题所在。一旦我能够使用真正的计算机,我会立即查看你的链接。非常感谢您的任何建议。 - Kevin
如果你阅读了 bootstrap.native 的演示页面,你很可能知道为什么它被称为 NATIVE。 - thednp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接