【问题标题】:Chrome Extension/Bootstrap Native Function not defined? (Javascript)Chrome 扩展/引导本机功能未定义? (Javascript)
【发布时间】:2017-07-25 06:15:18
【问题描述】:

似乎我无法访问脚本中的 .popover 方法。除非我搞砸了,否则我应该可以从包含的 Bootstrap Native(Jquery-free 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);
}

引导原生链接:

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 提供的文件甚至不是正确的文件。或者我没有正确调用该方法,但这不应该给我这个错误。

【问题讨论】:

  • 嗨。有测试页吗?在此之前,请确保 bootstrap.native.js 和/或您的脚本已加载到您的 body/app 容器的末尾。
  • @thednp 嘿!测试页面是我学校的注册页面。它很长,所以我将它作为一个新的评论。在那里,我只需选择要显示的课程的任何标准(我使用人类学系 + 正好 101),一旦你有结果,单击扩展图标并运行脚本。 (其他cmets继续回复)
  • 为了确保 bootstrap.native.js 加载到我的 cody/app 容器的末尾,您可以看到该页面不是我的。我对此很陌生,所以我可能无法正确理解您。据我所知,扩展程序应该将页面注入内容脚本运行的“孤立世界”(在 background.js 中)。我知道一些我见过嵌入/注入的代码?
  • 我相信我正确设置了页面,以便可以调用 popover。 Link to school's page 非常感谢您的帮助。不敢相信你真的回了我!
  • 该页面上应该发生什么?

标签: javascript twitter-bootstrap google-chrome-extension bootstrap-native


【解决方案1】:

基于此thread,确保将 jQuery 和 Bootstrap 依赖项添加到清单文件中。

当你调用方法popover时,可能还没有加载引导脚本。

你有两个选择:

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

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

【讨论】:

  • 依赖项应该包含在 bootstrap native 中,重点是避免使用 jquery。不要认为我需要使用该库添加引导程序依赖项,但文档是 garbo,所以我不确定。现在正在旅行,但如果我无法让该本地库正常工作,我会尝试使用 jQuery。如果没有加载该方法,我的 VS 代码 ide(我猜是 ide 吗?)至少会自动填充函数名称吗?这让我觉得图书馆是个问题。一旦我能找到一台真正的电脑,我就会检查你的链接。非常感谢您的任何建议。
  • 如果您阅读了bootstrap.native 演示页面,您可能会知道为什么它被称为 NATIVE。
猜你喜欢
  • 2011-10-23
  • 1970-01-01
  • 2013-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多