3

Seems like I can't access the .popover method inside my script. Unless I screwed up, I should have access to that method from the Bootstrap Native (Jquery-free Bootstrap) file included. ?

All the script does is add links and ideally a popover on that element.

Here is my code:

Manifest:

{
  "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!"
  }
}

Background: (runs on icon click)

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);
}

Link to bootstrap native:

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

and

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

Error:

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 is the 68kb file from their bootstrap native/dist/ folder you download. I think this could be the issue, because when I stick a variable in this file it is accessable inside the content script, but not the methods.

I'm brand freaking to new to all of this, so maybe the file I have for bootstrap native isn't even the correct one. Or I'm not calling the method correctly, but that shouldn't give me this error.

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
Kevin
  • 129
  • 11
  • Hi. Do you have a test page? Before that, make sure the `bootstrap.native.js` and/or your script is/are loaded at the end of your body/app container. – thednp Aug 07 '17 at 22:17
  • @thednp Hey! The test page is my school's registration page. It's long so I'll make it a new comment. When there, I'd just pick any criteria for classes to show up (I use Anthropology department + exactly 101) and once you have results, click the extension icon and the script runs. (Other comments continue reply) – Kevin Aug 08 '17 at 03:32
  • For making sure bootstrap.native.js is loaded at the end of my cody/app container, as you can see the page isn't mine. I'm very new to this, so I might not correctly understand you. The extension should be injecting the page into the 'isolated world' where the content script runs (in background.js) as far as I know. I know some code I've seen imbeds/injects? the – Kevin Aug 08 '17 at 03:35
  • I believe I'm setting the page up correctly so that popover can be called though. [Link to school's page](https://mycsuf.fullerton.edu/psp/pfulprd/EMPLOYEE/HFULPRD/c/SA_LEARNER_SERVICES.CLASS_SEARCH.GBL?PORTALPARAM_PTCNAV=FUL_POC_SA_COURSE_CATALOG_SRCH&EOPP.SCNode=EMPL&EOPP.SCPortal=EMPLOYEE&EOPP.SCName=PFUL_COURSE&EOPP.SCLabel=Catalog/Schedule%20of%20Classes&EOPP.SCPTcname=&FolderPath=PORTAL_ROOT_OBJECT.PORTAL_BASE_DATA.CO_NAVIGATION_COLLECTIONS.PFUL_COURSE.PFUL_S200708300709594181035798&IsFolder=falsehttp) Thank you so much for any help. Can't believe you actually got back to me! – Kevin Aug 08 '17 at 03:36
  • what is supposed to happen on that page? – thednp Aug 08 '17 at 08:37
  • @thednp Currently (unless it's not running?) it should link to the professor's ratemyprofessor ratings page. Additionally, once the popover works, it should display the ratemyprof page info on hover. I believe it is setting up the page's DOM correctly after the script runs (so popover can be correctly called), but the popover method itself 'isn't defined.' If it isn't working, let me know! – Kevin Aug 10 '17 at 05:26
  • In the source of [your page](https://mycsuf.fullerton.edu/psp/pfulprd/EMPLOYEE/HFULPRD/c/SA_LEARNER_SERVICES.CLASS_SEARCH.GBL?PORTALPARAM_PTCNAV=FUL_POC_SA_COURSE_CATALOG_SRCH&EOPP.SCNode=EMPL&EOPP.SCPortal=EMPLOYEE&EOPP.SCName=PFUL_COURSE&EOPP.SCLabel=Catalog/Schedule%20of%20Classes&EOPP.SCPTcname=&FolderPath=PORTAL_ROOT_OBJECT.PORTAL_BASE_DATA.CO_NAVIGATION_COLLECTIONS.PFUL_COURSE.PFUL_S200708300709594181035798&IsFolder=falsehttp) there is no mention of `bootstrap.native`. I cannot help with something I have no clue what's it supposed to do. – thednp Aug 10 '17 at 11:13
  • @thednp Hey, sorry I didn't respond for a few days. School stuff. On topic -There is no mention of bootstrap.native on the page. I was under the impression that I've included it in my chrome extension. Do I need to somehow inject it into the page? What the page does doesn't really matter; I just can't seem to use any bootstrap functions. (Don't mind explaining). I'm calling them correctly, but it's as if it was c++ and I forgot my #includes. You're absolutely correct that there is no mention of it. That page isn't mine as well. If I do need to inject it in the page somehow, please let me know! – Kevin Aug 15 '17 at 05:54
  • Yes, just make sure the library is included before `content_script.js` somehow, either via the Chrome extension API, require or whatever you choose. – thednp Aug 15 '17 at 09:43

1 Answers1

0

Based from this thread, make sure that you add the jQuery and Bootstrap dependencies into your manifest file.

When you call the method popover, maybe the bootstrap script was not yet loaded.

You have 2 options:

  1. To call the method on $(window).load - http://output.jsbin.com/qoteqe
  2. Include the bootstrap and jQuery references in the DOM and not load them in the script: http://output.jsbin.com/tamoda

Here are some related posts which might help:

abielita
  • 13,147
  • 2
  • 17
  • 59
  • the dependacies should be included with the bootstrap native, that whole point was to avoid using jquery. Don't think I need to add bootstrap dependencies with that library, but the documentation is garbo so I don't know for sure. On a trip right now, but if I can't get that native library working I'll try with jQuery. If the method wasn't loaded, wouldn't my VS code ide (ide i guess?) at least autofill the function name? It makes me think the library is the issue. Will check out your links as soon as I can get to a real computer. Thank you so much for any advice. – Kevin Aug 01 '17 at 04:02
  • If you read the `bootstrap.native` demo page, you will probably know why it's called NATIVE. – thednp Aug 08 '17 at 08:38