Skip to content

User scripts don't apply correctly in about:blank #15082

Closed

Description

@jdm

Our implementation of user scripts appends <script src='file://...'></script> to the <head> element in every document is parsed. This breaks the synchronous nature of about:blank, since that's an asynchronous network request that interrupts parsing. I have a patch that embeds the user script source as an inline script instead.

Activity

  1. added
    A-content/script
    I-papercut
    on Jan 17, 2017
  2. jdm commented on Jan 17, 2017

    @jdm
    MemberAuthor
    diff --git a/components/script/dom/userscripts.rs b/components/script/dom/userscripts.rs
    index 66758af..16a1b61 100644
    --- a/components/script/dom/userscripts.rs
    +++ b/components/script/dom/userscripts.rs
    @@ -5,14 +5,15 @@
     use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
     use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
     use dom::bindings::inheritance::Castable;
    -use dom::bindings::js::RootedReference;
    +use dom::bindings::js::{RootedReference, Root};
     use dom::bindings::str::DOMString;
     use dom::htmlheadelement::HTMLHeadElement;
     use dom::node::Node;
     use servo_config::opts;
     use servo_config::resource_files::resources_dir_path;
     use std::borrow::ToOwned;
    -use std::fs::read_dir;
    +use std::fs::{File, read_dir};
    +use std::io::Read;
     use std::path::PathBuf;
     
     
    @@ -40,12 +41,18 @@ pub fn load_script(head: &HTMLHeadElement) {
             files.sort();
     
             for file in files {
    -            let name = match file.into_os_string().into_string() {
    +            /*let name = match file.into_os_string().into_string() {
                     Ok(ref s) if s.ends_with(".js") => "file://".to_owned() + &s[..],
                     _ => continue
    -            };
    +        };*/
    +            //let name = file.into_os_string().into
    +            let mut f = File::open(&file).unwrap();
    +            let mut contents = vec![];
    +            f.read_to_end(&mut contents).unwrap();
                 let new_script = doc.CreateElement(DOMString::from("script")).unwrap();
    -            new_script.set_string_attribute(&local_name!("src"), DOMString::from(name));
    +            let script_text = Root::upcast(doc.CreateTextNode(String::from_utf8(contents).unwrap().into()));
    +            new_script.upcast::<Node>().AppendChild(&*script_text).unwrap();
    +            //new_script.set_string_attribute(&local_name!("src"), DOMString::from(name));
                 node.InsertBefore(new_script.upcast(), first_child.r()).unwrap();
             }
         }
  3. added
    C-has-patch
    on Jan 17, 2017
  4. Ms2ger commented on Jan 19, 2017

    @Ms2ger
    Contributor

    At this point, why not just call evaluate_js_script or whatever it's called?

  5. jdm commented on Jan 19, 2017

    @jdm
    MemberAuthor

    That's... a good point!

  6. added
    E-less-complex
    and removed
    C-has-patch
    on Feb 8, 2017
  7. highfive commented on Feb 8, 2017

    @highfive

    Hi! If you have any questions regarding this issue, feel free to make a comment here, or ask it in the #servo channel in IRC.

    If you intend to work on this issue, then add @highfive: assign me to your comment, and I'll assign this to you. 😄

  8. jdm commented on Feb 8, 2017

    @jdm
    MemberAuthor

    The best way to test this change:
    Modify resources/user-agent-js/00.example.js to contain window.foo = 'success!';, then run ./mach run test.html --userscripts, where test.html is a page like:

    <script>console.log(window.foo);</script>
    <iframe></iframe>
    <script>console.log(document.querySelector('iframe').contentWindow.foo);</script>

    The output should be:

    success!
    success!
    

    but on master right now I see

    undefined
    undefined
    
  9. jdm commented on Feb 8, 2017

    @jdm
    MemberAuthor

    We'll want to call GlobalScope::evaluate_js_on_global_with_result instead of appending a <script> element to the document.

  10. meetmangukiya commented on Feb 12, 2017

    @meetmangukiya
    Contributor

    @highfive: assign me

  11. added
    C-assigned
    on Feb 12, 2017
  12. highfive commented on Feb 12, 2017

    @highfive

    Hey @meetmangukiya! Thanks for your interest in working on this issue. It's now assigned to you!

  13. removed
    C-assigned
    on Mar 4, 2017
  14. jdm commented on Mar 4, 2017

    @jdm
    MemberAuthor

    Unassigning due to inactivity.

  15. dudelson commented on Mar 4, 2017

    @dudelson

    @highfive: assign me

  16. highfive commented on Mar 4, 2017

    @highfive

    Hey @dudelson! Thanks for your interest in working on this issue. It's now assigned to you!

  17. added
    C-assigned
    on Mar 4, 2017
  18. dudelson commented on Mar 8, 2017

    @dudelson

    Although I would like to work on this issue, I've become too busy to complete it at this time. Please unassign me.

  19. ghost removed
    C-assigned
    on Mar 8, 2017
  20. sendilkumarn commented on Mar 8, 2017

    @sendilkumarn
    Contributor

    @highfive: assign me

  21. highfive commented on Mar 8, 2017

    @highfive

    Hey @sendilkumarn! Thanks for your interest in working on this issue. It's now assigned to you!

  22. added
    C-assigned
    on Mar 8, 2017
  23. added a commit that references this issue on Mar 13, 2017
    d70c1e5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/scriptC-assignedE-less-complexI-papercut

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions