web-extensions-rs / web-extensions

Rust wrappers for WebExtensions API
https://crates.io/crates/web-extensions
MIT License
34 stars 6 forks source link

Cannot read properties of undefined (reading 'search') #10

Closed TieWay59 closed 1 year ago

TieWay59 commented 1 year ago

Hi, I'm trying to get into this crate and make some web extension demos. I found that web_extensions::history::search not working as I expected and threw an error of properties of undefined. I did my investigation for a day, but I‘m new to both wasm&web-extension so I still have not solved this problem. More details will show below:

My main logic is as below, it's very simple:

#[wasm_bindgen(start)]
pub async fn main() {
    std::panic::set_hook(Box::new(console_error_panic_hook::hook));

    let millis_since_epoch = js_sys::Date::now() as i64; // the number of milliseconds elapsed since EPOCH

    let one_hour_earlier = Some(millis_since_epoch - 60 * 60 * 1000);

    let now = Some(millis_since_epoch);

    let q = Query {
        text: "Query",
        start_time: one_hour_earlier,
        max_results: Some(3),
        end_time: now,
    };

    let history_items = search(&q)
        .await
        .expect("Unknow `web_extensions::history::search` bug.");

    // log shows it's stuck here.
}

The search function is an alias of web_extensions::history::search.

pub async fn search(query: &Query<'_>) -> Result<Vec<HistoryItem>, Error> {
    let js_query = js_from_serde(query)?;
    let js_value = sys::chrome()
        .history()
        .search(object_from_js(&js_query)?)
        .await;
    serde_from_js(js_value)
}

In the generated js file, the error line is:

imports.wbg.__wbg_search_5602fc53d1278731 = function (arg0, arg1) {
  const ret = getObject(arg0).search(getObject(arg1)); // <-- this `.search` is properties of undefined.
  return addHeapObject(ret);
};

I see this means the object taken from the heap does not contain the search property.

Version Info

Google Chrome: 107.0.5304.107 wasm-bindgen = "0.2.83" js-sys = "0.3" wasm-bindgen-futures = "0.4" console_error_panic_hook = "0.1.6" web-extensions = "0.3.0"

I'm not sure any other information is helpful if provided, let me know if you need more to locate the error.

flosse commented 1 year ago

Could you post your manifest.json?

TieWay59 commented 1 year ago

Thank you for your fast reply @flosse. Here is my manifest file:

{
  "name": "amazing-extension",
  "version": "1.0",
  "description": "amazing-extension",
  "permissions": [],
  "content_scripts": [
    {
      "matches": ["*://*.example.com/*"],
      "js": ["amazing_extension.js", "run_wasm.js"]
    }
  ],
  "web_accessible_resources": [
    {
      "resources": ["amazing_extension_bg.wasm"],
      "matches": ["*://*.example.com/*"]
    }
  ],
  "manifest_version": 3
}

And let me add more details on how to reproduce it.

  1. I use Mubelotix/wasm-extension-template
  2. It only requires cargo-generate and wasm-pack.
  3. Then run cargo generate --git https://github.com/Mubelotix/wasm-extension-template --name amazing-extension
  4. The default project setup will contain a build.sh, which I will run it by sh build.sh --manifest-version=3 because google chrome won't accept manifest2 in latest version.
  5. Then I add web-extensions = "0.3.0" in cargo.toml
  6. And write the plain code logic in src\lib.rs -- pub async fn main(), where I only introduced the use of use web_extensions::history::{search, Query};
  7. And then I run it by sh build.sh --manifest-version=3
  8. Load the unpacked extension into my chrome.
  9. Then Open the example.com and see the console log print the results.

It's strange that, if I only print the web_extensions::history::Query instance, the result will be correct. But it seems the search function is just not found.

I hope this would help.

TieWay59 commented 1 year ago

Here are the contents of the console log, it shows the query object is printed out correctly. But it can't find the history search attribute.

image

amazing_extension.js:313 Query {
    text: "Query",
    end_time: Some(
        1669120728435,
    ),
    max_results: Some(
        3,
    ),
    start_time: Some(
        1669117128435,
    ),
}
amazing_extension.js:279 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'search')
    at imports.wbg.__wbg_search_5602fc53d1278731 (amazing_extension.js:279:36)
    at amazing_extension_bg.wasm:0x678f
    at amazing_extension_bg.wasm:0x13d16
    at amazing_extension_bg.wasm:0xea07
    at amazing_extension_bg.wasm:0x170e0
    at __wbg_adapter_18 (amazing_extension.js:210:10)
    at real (amazing_extension.js:195:20)
flosse commented 1 year ago

try to add "history" to the permissions.

TieWay59 commented 1 year ago

@flosse It still prints the same error message. But I didn't think about the manifest before, I'll investigate it. If you get any more ideas please let me know, this problem has bothered me for a day.

TieWay59 commented 1 year ago

Oh, after searching in developer.chrome.com, I think I found my answer.

According to https://developer.chrome.com/docs/extensions/mv3/content_scripts/#capabilities, it was said content scripts are unable to access other APIs directly(including history I guess).

I'll make some changes in the template project tomorrow, and maybe use this API in the background.

Sorry for the unrelated issue here, this is caused by my lacking of extension knowledge. I'll close it myself after I test my approach.