Add-on beginner question

  • I have looked at the Hellow World, pop up, and tabs developer pages.

    I am attempting to create a combination now without success.

    What I want is a popup, that has a link, when I click the link, it should update the current tab.

    So, I was thinking a script on the html page, using the script for tabs.

    There are obstacles:
    1 - the script code adds a listener. Can I add a listener to my link in the web page? I don't need a listener, I simply want to say, user clicked, you go and update the page.

    2 - the script is diabled by security. I have enabled tabs in the manifest but this is beyond that.

    I get:

    Refused to execute inline event handler because it violates the following content security policy directive: script-src self blob filesystem chrome-extension-resource. Either the unsafe-inline keyword, a hash sha256.. or a nonce nonce- is required to enable inline execution.

    Could you help me?

    (sorry for the double post)

    (if it matters I'm on opera 38)

  • ok so I put my javascript code outside the html to avoid security problems and do not get that error anymore, but it's like the js file does not access the html file. I've tried both:

    document.getElementById('linkspan').appendChild(link);
    document.getElementsByTagName("body")[0].appendChild(link)

    but get:

    uncaught typer error: Cannot read property appendChild of null
    uncaught typer error: Cannot read property appendChild of undefined

  • when I enumerate the page with:
    for (i = 0; i < document.all.length; i++) {
    alert(document.all[i].nodeName);

    I get:
    HTML, HEAD, STYLE, SCRIPT only...

    no body!!

    my html is very simple, I don't know how to post the source in this forum... O.O

  • my html is very simple, I don't know how to post the source in this forum.

    Store it somewhere and put the link here.

    Maybe this page has some info for you.

  • thank you leoc, that was bc can't use onload trigger in page, so script tag had to come last in the html page.

    now back to original question, how do I from javascript referenced from my popup page, change location of active tab?

    the link in the popup page, calls a javascript function onclick, if I try to set chrome.tab[0].url the tab[0] is undefined

  • First of all, you don't change url directly and you need permissions for tabs in your manifest file, to have access to tabs API
    add this to manifest.json file:

    "permissions": ["tabs"]

    Then when you wan't to change url in active tab, you have to do it in two steps,
    get active tabId and inside of callback function you can change url, like so:

    chrome.tabs.query({active: true}, function(tabs){
    chrome.tabs.update(tabs[0].id, {url: "your url here"}, function(){});
    });

    Anyway you should take a look at chrome's API:
    https://developer.chrome.com/extensions/tabs#method-query

    Hope that helps. 😄

Log in to reply
 

Looks like your connection to Opera forums was lost, please wait while we try to reconnect.