最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How To Get Input Value In Chrome Extension? - Stack Overflow

programmeradmin4浏览0评论

I have a site which have this element :

<input type="text" id="editpane_title" fnk="tt" maxlength="80" name="title" value="st benedict crucifix">

And I want to get and set value of that input using Chrome Extension :

{
  "manifest_version": 2,
  "name": "demo",
  "version": "1.0",
  "description": "demo",
  "icons": { "16": "icon16.png",
             "48": "icon48.png",
             "128": "icon128.png" },

  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },

  "permissions": [ "tabs" ],

  "browser_action": {
      "default_icon": {
        "16": "icon16.png",
        "24": "icon24.png",
        "32": "icon32.png"
      },
      "default_title": "demo"
    }
}

and here's background.js :

chrome.browserAction.onClicked.addListener(function(tab) {

    chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) {

        var title = document.getElementById('editpane_title').value;
        alert(title);

    });


});

and the alert is not showing up at all. I tried reload multiple times but still failed.

what did I missed here?

I have a site which have this element :

<input type="text" id="editpane_title" fnk="tt" maxlength="80" name="title" value="st benedict crucifix">

And I want to get and set value of that input using Chrome Extension :

{
  "manifest_version": 2,
  "name": "demo",
  "version": "1.0",
  "description": "demo",
  "icons": { "16": "icon16.png",
             "48": "icon48.png",
             "128": "icon128.png" },

  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },

  "permissions": [ "tabs" ],

  "browser_action": {
      "default_icon": {
        "16": "icon16.png",
        "24": "icon24.png",
        "32": "icon32.png"
      },
      "default_title": "demo"
    }
}

and here's background.js :

chrome.browserAction.onClicked.addListener(function(tab) {

    chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) {

        var title = document.getElementById('editpane_title').value;
        alert(title);

    });


});

and the alert is not showing up at all. I tried reload multiple times but still failed.

what did I missed here?

Share Improve this question asked Sep 28, 2017 at 4:25 Saint RobsonSaint Robson 5,52518 gold badges74 silver badges121 bronze badges 3
  • 2 Either use a content script or use chrome.tabs.executeScript in background js. – Aefits Commented Sep 28, 2017 at 5:51
  • Also Check th it here: stackoverflow./questions/52455598/… – 31113 Commented Sep 22, 2018 at 10:53
  • Also check the solution here: https://stackoverflow./questions/52455598/get-input-value-chrome-extension/52455916#52455916 – 31113 Commented Sep 22, 2018 at 10:54
Add a ment  | 

1 Answer 1

Reset to default 8

Two issues here.

First off, code not working and you have no idea why.

alert is not showing up at all

You need to learn to debug extensions to understand what's wrong. Trying to solve the problem yourself is important even if you fail to do so. There's Google's own somewhat dated tutorial that mostly covers popups, and there's this canonical question for background scripts:

Where to read console messages from background.js in a Chrome extension?


So suppose you follow the advice and try debugging. You will quickly learn that alert is not called, because there's an exception:

Uncaught TypeError: Cannot read property 'value' of null

That leads to a second problem: document.getElementById('editpane_title') can't find the element.

Ask yourself: which document is referenced here? Or rather, why would it automagically be the page you're looking at?

At this point, you should read the Architecture Overview. You'll learn there:

  • The background scripts actually live in a separate, invisible tab in their own document.
  • To access a visible page's document, you need a Content Script.
  • To pass data between them, you have to employ Messaging and/or storage API.

Armed with that knowledge, you should now turn to the following canonical question:

How to access the webpage DOM rather than the extension page DOM?


As a final point, you should not be using alert to inspect the state of your program, or for any reason really.

This ancient mechanism is blocking: it stops JS context from doing anything until you dismiss the modal window. This screws up with some extension APIs that are asynchronous and interface with native ponents of the browser.

In fact, in Firefox WebExtensions you expicitly can't use them from a background page.

So, wean off this bad habit early:

  • The equivalent of printf/echo-style debugging is console.log(), with the output accessible as explained in the debugging section.
  • To alert the user, you should seek other UI capabilities, such as notifications.
发布评论

评论列表(0)

  1. 暂无评论